如何在AsyncTask中加载imageViews

时间:2016-07-12 17:54:34

标签: java android android-fragments android-asynctask progress-bar

我的应用程序中有ViewPager,其中包含7个片段,每个片段包含多个图像。 我想在每个片段加载它的图像时显示ProgressBar。 所以我创建了AsyncTask,在后台加载图像。 问题是我无法用Picasso库加载图像它向我显示了一些错误。 有人可以帮我弄清楚怎么做吗?

编辑:我之所以这么说是因为用毕加索加载所有图片大约需要1.5秒,看起来非常迟钝。随着“android:src”应用程序崩溃,因为OutOfMemory问题,“应用程序可能在其主线程上做了太多工作。”所以我正在寻找更好的方法。

谢谢!!!

fragment1.java:

public class Fragment1 extends Fragment {

public Fragment1() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {

     View rootView = inflater.inflate(R.layout.fragment_frag1, container, false);

    LoadImagesTask loadImagesTask = new LoadImagesTask();
    loadImagesTask.execute();

                // Inflate the layout for this fragment
        return rootView;
}

private class LoadImagesTask extends AsyncTask<Void, Void, Void> {
        ProgressBar progressBar = (ProgressBar) getActivity().findViewById(R.id.loading);
        ImageView image1 = (ImageView) getActivity().findViewById(R.id.imageView_S0P1);
        ImageView image2 = (ImageView) getActivity().findViewById(R.id.imageView_S0P2);
        ImageView image3 = (ImageView) getActivity().findViewById(R.id.imageView_S0P3);
        ImageView image4 = (ImageView) getActivity().findViewById(R.id.imageView_S0P4);
        ImageView image5 = (ImageView) getActivity().findViewById(R.id.imageView_S0P5);
        ImageView image6 = (ImageView) getActivity().findViewById(R.id.imageView_S0P6);
        ImageView image7 = (ImageView) getActivity().findViewById(R.id.imageView_S0P7);
        ImageView image8 = (ImageView) getActivity().findViewById(R.id.imageView_S0P8);
        ImageView image9 = (ImageView) getActivity().findViewById(R.id.imageView_S0P9);
        ImageView image10 = (ImageView) getActivity().findViewById(R.id.imageView_S0P10);

    @Override
    protected Void doInBackground(Void... params) {

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Picasso.with(getActivity()).load(R.drawable.s0p1).into(image1);
                Picasso.with(getActivity()).load(R.drawable.s0p2).into(image2);
                Picasso.with(getActivity()).load(R.drawable.s0p3).into(image3);
                Picasso.with(getActivity()).load(R.drawable.s0p4).into(image4);
                Picasso.with(getActivity()).load(R.drawable.s0p5).into(image5);
                Picasso.with(getActivity()).load(R.drawable.s0p6).into(image6);
                Picasso.with(getActivity()).load(R.drawable.s0p7).into(image7);
                Picasso.with(getActivity()).load(R.drawable.s0p8).into(image8);
                Picasso.with(getActivity()).load(R.drawable.s0p9).into(image9);
                Picasso.with(getActivity()).load(R.drawable.s0p10).into(image10);
            }
        });

        return null;
    }

    @Override
    protected void onPostExecute(Void event) {
        progressBar.setVisibility(View.GONE);
    }
}
}

logcat的:

07-12 17:03:32.838 17497-17581/appinventor.ai_itiel_maimon.Rubiks_cube E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
                                                                                     Process: appinventor.ai_itiel_maimon.Rubiks_cube, PID: 17497
                                                                                     java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                         at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                                         at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                         at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                         at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                         at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                         at java.lang.Thread.run(Thread.java:818)
                                                                                      Caused by: java.lang.IllegalStateException: Method call should happen from the main thread.
                                                                                         at com.squareup.picasso.Utils.checkMain(Utils.java:136)
                                                                                         at com.squareup.picasso.RequestCreator.into(RequestCreator.java:615)
                                                                                         at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
                                                                                         at appinventor.ai_itiel_maimon.Rubiks_cube.Intro$LoadImagesTask.doInBackground(Intro.java:68)
                                                                                         at appinventor.ai_itiel_maimon.Rubiks_cube.Intro$LoadImagesTask.doInBackground(Intro.java:36)
                                                                                         at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                                         at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                         at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                                         at java.lang.Thread.run(Thread.java:818) 

2 个答案:

答案 0 :(得分:0)

AsyncTask背后的想法是doInBackground执行后台工作,然后你可以返回一些将作为参数传递给onPostExecute的东西,它在主线程中执行。 所以尝试从onPostExecute()方法设置图像:) 但是你可以在不使用AsyncTask的情况下轻松完成,只需这样做:

QString str = "spinbox_1";

switch stoi( str )
case 1543:
    //code here
case 2343:
    //code here
case 3424:
    //code here

答案 1 :(得分:0)

如果你想得到一个好的答案,你应该在这里发布一些代码。
一般来说,你可以简单地将drawable用作ImageView xml实体中的“android:src”(这里你只使用程序化方法做了几乎相同的事情......) 或者在Main \ UI线程上使用Picasso(它与负责渲染UI组件的线程的名称相同),即在inflate方法上。


一般而言,尝试并检查一些“如何”使用片段和一般的android设计模式的教程,你显然还在学习,但使用谷歌重新定位的方法将简化你的学习并减少你的错误。< / p>