When calling Picasso.with(MainActivity.this).load(captchaImg).into(img); throw an exception saying java.lang.IllegalArgumentException: Target must not be null. I understand that he does not have enough ImageView , but I gave it to him, announced it, and he still sees it as null , why?

Transferred to the main main_layout , is displayed, and in AlertDialog(captcha_layout) when I try to pass a link to img ... it sees as null. Can somehow not truly work with AlertDialog ?

 public class MainActivity extends AppCompatActivity { private String url; private String captchaImg; private ImageView img; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img = (ImageView) findViewById(R.id.imageView) btn_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new ShowDialogAsyncTask().execute(); } }); } private class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void> { @Override protected void onPostExecute(Void result) { super.onPostExecute(result); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Важное сообщение!") .setView(R.layout.captcha_layout); AlertDialog alert = builder.create(); alert.show(); Picasso.with(MainActivity.this).load(captchaImg).into(img); } Toast.makeText(MainActivity.this, "Вызов onPostExecute()", Toast.LENGTH_SHORT).show(); txt_percentage.setText(accesToken); btn_start.setEnabled(true); } 

captchaImg - contains uri images, there are no problems here, it loads.

enter image description here

  • Does the debugger show that in onPostExecute(...) img == null ? - post_zeew
  • Yes, img == null - Heaven
  • And in onCreate(...) it not null ? - post_zeew
  • Also null ... but I only have to declare the ImageView in main_layout so immediately displayed and cease to be null - Heaven
  • one
    So you are not there looking for your ImageView . - post_zeew

1 answer 1

Everything is clear here, your imageView is not in the layout that you need, you look for it in activity_main.xml, and it is in captcha_layout.xml. Do the following:

 View alertDialogView = LayoutInflater.from(context).inflate(R.layout.captcha_layout); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Важное сообщение!") .setView(alertDialogView ); AlertDialog alert = builder.create(); alert.show(); Picasso.with(MainActivity.this).load(captchaImg).into((ImageView)alertDialogView.findViewById(id));