android - doInBackground 中的新 LazyAdapter

标签 android android-asynctask android-lazyadapter

我在 doInBackground (用于加载屏幕)中使用 XML 解析,然后在 listview(list) 中使用 LazyAdapter 设置它。

首先是代码示例:

public class NewCoupons extends Activity {

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
             
        bar = (ProgressBar) this.findViewById(R.id.bar);
        new ProgressTask().execute();
}

private class ProgressTask extends AsyncTask <Void,Void,Void>{

        @Override
        protected Void doInBackground(Void... arg0) {   
          .....
          list=(ListView)findViewById(R.id.list);
          adapter = new LazyAdapter(this, xy);        
          list.setAdapter(adapter);
        }
}
}

当我使用“NewCoupons.this”时,我变成:

05-15 21:01:16.553: E/AndroidRuntime(6319): FATAL EXCEPTION: AsyncTask

1 05-15 21:01:16.553: E/AndroidRuntime(6319): java.lang.RuntimeException: An error occured while executing

doInBackground() 05-15 21:01:16.553: E/AndroidRuntime(6319): at

android.os.AsyncTask$3.done(AsyncTask.java:299) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)

05-15 21:01:16.553: E/AndroidRuntime(6319): at

java.util.concurrent.FutureTask.setException(FutureTask.java:219)

05-15 21:01:16.553: E/AndroidRuntime(6319): at

java.util.concurrent.FutureTask.run(FutureTask.java:239) 05-15

21:01:16.553: E/AndroidRuntime(6319): at

java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)

05-15 21:01:16.553: E/AndroidRuntime(6319): at

java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)

05-15 21:01:16.553: E/AndroidRuntime(6319): at

java.lang.Thread.run(Thread.java:856) 05-15 21:01:16.553:

E/AndroidRuntime(6319): Caused by:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the

original thread that created a view hierarchy can touch its views.

05-15 21:01:16.553: E/AndroidRuntime(6319): at

android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746) 05-15

21:01:16.553: E/AndroidRuntime(6319): at

android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823) 05-15

21:01:16.553: E/AndroidRuntime(6319): at

android.view.View.requestLayout(View.java:15473) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

android.view.View.requestLayout(View.java:15473) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

android.view.View.requestLayout(View.java:15473) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

android.view.View.requestLayout(View.java:15473) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

android.view.View.requestLayout(View.java:15473) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

android.widget.AbsListView.requestLayout(AbsListView.java:1819) 05-15

21:01:16.553: E/AndroidRuntime(6319): at

android.widget.ListView.setAdapter(ListView.java:490) 05-15

21:01:16.553: E/AndroidRuntime(6319): at

com.example.androidhive.NewCoupons$ProgressTask.doInBackground(NewCoupons.java:170)

05-15 21:01:16.553: E/AndroidRuntime(6319): at

com.example.androidhive.NewCoupons$ProgressTask.doInBackground(NewCoupons.java:1)

05-15 21:01:16.553: E/AndroidRuntime(6319): at

android.os.AsyncTask$2.call(AsyncTask.java:287) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

java.util.concurrent.FutureTask.run(FutureTask.java:234) 05-15

21:01:16.553: E/AndroidRuntime(6319): ... 3 more

问题:是我无法在新的 LazyAdapter 上使用“this”,这将是一个错误。 我必须在新的 LazyAdapter 中为“this”设置什么?

谢谢。

最佳答案

您必须传递上下文。这意味着,如果您在 fragment 中,您可以执行以下操作:MyClassName.thisgetActivity()

编辑:

这是您的错误:

Only the original thread that created a view hierarchy can touch its views.

关于android - doInBackground 中的新 LazyAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16574806/

相关文章:

android - 从 Activity 完成异步后在 Fragment 上填充 ListView

Android无限循环进度对话框AsyncTask

android - 在 ListView 中显示数据和图像

java - android.view.ViewRootImpl$CalledFromWrongThreadException : Only the original thread that created a view hierarchy can touch its views

android - VideoJS 不适用于移动设备

android - 方向锁定

java - 将 2D Mat 对象转换为 1D 数组

java - 显示启动画面直到应用加载完成

android - ListView 崩溃应用程序 :Java null Pointer Exception

android - 使用 React Native 下载数据文件以供离线使用