In the array of three images (URL), upload and paste into imageView. I click on the button and nothing happens. Permission to the Internet in the manifesto gave. Where is the joint. In the log is

04-02 02:16:23.588 4211-4211/com.example.user.copywork8 E/Zygote: Zygote: error closing descriptor libcore.io.ErrnoException: close failed: EBADF (Bad file number) at libcore.io.Posix.close(Native Method) at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75) at com.android.internal.os.ZygoteInit.closeServerSocket(ZygoteInit.java:221) at com.android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:879) at com.android.internal.os.ZygoteConnection.runOnce(ZygoteConnection.java:242) at com.android.internal.os.ZygoteInit.runSelectLoop(ZygoteInit.java:715) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651) at dalvik.system.NativeStart.main(Native Method) 04-02 02:17:07.455 4211-4211/com.example.user.copywork8 E/ffi_jank: timespan = 26.238924 

code.

 public class MainActivity extends AppCompatActivity { TextView textView; ProgressBar progressBar; ImageView imageView1, imageView2, imageView3; ArrayList<String> arrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); progressBar = (ProgressBar) findViewById(R.id.progress); imageView1 = (ImageView) findViewById(R.id.img1); imageView2 = (ImageView) findViewById(R.id.img2); imageView3 = (ImageView) findViewById(R.id.img3); arrayList = new ArrayList<>(); arrayList.add("http://startandroid.ru/images/stories/pic/android_white.png"); arrayList.add("http://allboxing.ru/sites/default/files/logo_4.png"); arrayList.add("http://static.akipress.org/akipress_logo2.png"); } public void onClick(View view) { new MyTask(arrayList); } private class MyTask extends AsyncTask<Void, Void, ArrayList<Bitmap>> { ArrayList<String> arrayList; public MyTask(ArrayList<String> arrayList){ this.arrayList = arrayList; } @Override protected void onPreExecute() { super.onPreExecute(); progressBar.setVisibility(View.VISIBLE); } @Override protected ArrayList<Bitmap> doInBackground(Void... params) { ArrayList<Bitmap> bitmaps = new ArrayList<>(); for (String image : arrayList){ try { URL url = new URL(image); InputStream is = url.openConnection().getInputStream(); bitmaps.add(BitmapFactory.decodeStream(is)); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return bitmaps; } @Override protected void onPostExecute(ArrayList<Bitmap> bitmaps) { super.onPostExecute(bitmaps); progressBar.setVisibility(View.INVISIBLE); imageView1.setImageBitmap(bitmaps.get(0)); imageView2.setImageBitmap(bitmaps.get(1)); imageView3.setImageBitmap(bitmaps.get(2)); } } } 

Ps I'm new, please without abstruse terms. If so, with explanations)

  • one
    Forgot to call execute from task. - zRrr
  • Yes, since I used the constructor. So said teacher. It is exactly necessary to call it without execute, by calling directly new MyTasc (arrayList) - T. Kudaibergen

1 answer 1

You need to run the task after its creation.

  • new MyTask(arrayList).execute();

or so (run task from task constructor)

 public MyTask(ArrayList<String> arrayList){ this.arrayList = arrayList; execute(); } 

In the second case, it is useful to make MyTask a final-class, because when inheriting from it, the task will be executed before the successor constructor works.

  • Thank you very much, the second option was needed) worked)) - T. Kudaibergen
  • @ T.Kudaibergen, welcome to ru-SO) - Yuriy SPb
  • one
    in the second case, you need to at least make MyTask final class, because when inheriting from it, the task will be executed before the successor constructor works. - zRrr
  • @zRrr, right. I added this in response) - YuriySPb