简体   繁体   English

Android:显示进度栏时出现AsyncTask错误

[英]Android: AsyncTask Error When showing Progress Bar

In my application one button and one ImageView is there. 在我的应用程序中,有一个按钮和一个ImageView。 After clicking on button it get the photo from server and display on inmageView. 单击按钮后,它将从服务器获取照片并显示在inmageView中。 At the time of getting photo from server it show progress bar. 从服务器获取照片时,它会显示进度栏。

This application able to get photo from server but not able to display on imageview. 此应用程序可以从服务器获取照片,但不能在imageview上显示。

code (for button): 代码(用于按钮):

@Override
public void onClick(View v) {
    switch (v.getId()) {
         case R.id.buttonFetchPhoto:
             new BackgroundTask().execute("Main");
             break;
    }
}

code(for progress bar) 代码(用于进度条)

class BackgroundTask extends AsyncTask<String , Integer, Void>
{
    @Override
    protected void onPreExecute()
    {
        bar = new ProgressDialog(activity.this);
        bar.setMessage("Processing..");
        bar.setIndeterminate(true);
        bar.show();

    } 
    @Override
    protected Void doInBackground(String... params) 
    {   
        // Code to get Photo Byte Array from server

        Bitmap bm = BitmapFactory.decodeByteArray(photoByteArray, 0,photoByteArray.length);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        imageViewGetPhoto.setImageBitmap(bm); //imageViewGetPhoto is define in activity class
        }

        return null;
    }
    @Override
    protected void onPostExecute(Void result) 
    {   
        bar.dismiss();
      }
}

I am getting error because of " imageViewGetPhoto.setImageBitmap(bm) " 由于出现“ imageViewGetPhoto.setImageBitmap(bm) ”,因此出现错误

I think this is because " imageViewGetPhoto " is defined into the layout class which is main thread and this thread not able to synchronize with main thread. 我认为这是因为“ imageViewGetPhoto ”已定义到作为主线程的布局类中,并且该线程无法与主线程同步。

How I get the data into Main thread which is retrieved by other thread. 我如何将数据放入由其他线程检索的主线程中。

Or how I access objects into other thread which is defined in main layout. 或者我如何将对象访问到主布局中定义的其他线程中。

Error: 错误:

FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)          
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original      thread that created a view hierarchy can touch its views.
at android.view.ViewRoot.checkThread(ViewRoot.java:3020)
at android.view.ViewRoot.requestLayout(ViewRoot.java:634)
at android.view.View.requestLayout(View.java:8267)
at android.view.View.requestLayout(View.java:8267)
at android.view.View.requestLayout(View.java:8267)
at android.view.View.requestLayout(View.java:8267)
at android.widget.ScrollView.requestLayout(ScrollView.java:1580)
at android.view.View.requestLayout(View.java:8267)
at android.widget.TableLayout.requestLayout(TableLayout.java:226)
at android.view.View.requestLayout(View.java:8267)
at android.view.View.requestLayout(View.java:8267)
at android.widget.TableLayout.requestLayout(TableLayout.java:226)
at android.view.View.requestLayout(View.java:8267)
at android.view.View.requestLayout(View.java:8267)
at android.widget.ImageView.setImageDrawable(ImageView.java:322)
at android.widget.ImageView.setImageBitmap(ImageView.java:336)
at com.pixel.potholelocator.PohholeLocatorActivity$BackgroundTask.doInBackground(PohholeLocatorActivity.java:295)
at com.pixel.potholelocator.PohholeLocatorActivity$BackgroundTask.doInBackground(PohholeLocatorActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)

Update your image view in 在中更新图像视图

 @Override
 protected void onPostExecute(Void result) {    
      //update your imageview 
 }

we can't update imageview in worker thread. 我们无法在工作线程中更新imageview。

Moveany modification of the ui like imageViewGetPhoto.setImageBitmap(bm); ui的moveany修改,例如imageViewGetPhoto.setImageBitmap imageViewGetPhoto.setImageBitmap(bm); into onPostExecute .. 进入onPostExecute ..

protected void onPostExecute(Void result) 
{   
    bar.dismiss();
    imageViewGetPhoto.setImageBitmap(bm);
  }

Write these 3 lines in 将这三行写在

@Override
    protected void onPostExecute(Void result) 
    { 
        bar.dismiss();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        imageViewGetPhoto.setImageBitmap(bm); 

     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM