无法从AsyncThread设置ListView适配器

时间:2012-03-12 14:45:48

标签: android

我在ListView上使用Activity,从SQLite数据库加载需要一段时间,所以我想向用户展示一个ProgressDialog让他们知道正在装货。我试图在一个单独的线程上运行任务,但我得到一个CalledFromWrongThreadException。这是我的主要Activity代码:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    try
    {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
        setContentView(R.layout.open_issues);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

        //Set Window title.
        final TextView title = (TextView) findViewById(R.id.customTitle);
        if (title != null)
            title.setText("Open Issues");

        //Call Async Task to run in the background.
        new LoadIssuesTask().execute();
    }
    catch (Exception e)
    {
        Errors.LogError(e);
    }
}

LoadIssuesTask代码:

private class LoadIssuesTask extends AsyncTask<Void, Void, Cursor> {

    ProgressDialog pdDialog = null;
   protected void onPreExecute()
   {
       try
       {
           pdDialog = new ProgressDialog(OpenIssues.this);
           pdDialog.setMessage("Loading Issues and Activities, please wait...");
           pdDialog.show();
       }
       catch (Exception e)
       {
           Errors.LogError(e);
       }
   }

   @Override
   protected Cursor doInBackground(Void... params) {
       LoadIssues();
       return null;
   }

   @Override
   protected void onPostExecute(Cursor c) {
       pdDialog.dismiss();
       pdDialog = null;
   }
 }

和LoadIssues代码:

private void LoadIssues(){
    //Set listview of Issues.
    ListView lvIssues = (ListView)findViewById(R.id.lvIssues);
    lvIssues.setOnItemClickListener(viewIssuesListener);

    IssueCreator = new IssueInfoCreator(this, Integer.parseInt(AppPreferences.mDBVersion));
    IssueCreator.open();
    lvIssues.setAdapter(new IssueInfoAdapter(this, IssueCreator.queryAll()));        
    IssueCreator.close();
}

IssueInfoAdapter的构造函数:

public IssueInfoAdapter(Context c, List<IssueInfo> list){
    mListIssueInfo = list;

    //create layout inflater.
    mInflater = LayoutInflater.from(c);
}

它在LoadIssues()内的.setAdapter方法上抛出了错误。 ERROR:

03-12 10:41:23.174: E/AndroidRuntime(11379): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: 
Only the original thread that created a view hierarchy can touch its views.

2 个答案:

答案 0 :(得分:10)

您尝试访问doInBackground方法中未在主UI主题上运行的视图。您必须在onPostExecute线程上运行的方法UI中设置适配器:

@Override
   protected void onPostExecute(List<IsueInfo> items) {
       pdDialog.dismiss();
       ListView lvIssues = (ListView)findViewById(R.id.lvIssues);
       lvIssues.setOnItemClickListener(viewIssuesListener);
       lvIssues.setAdapter(new IssueInfoAdapter(this, items));
   }

并在您的doInBackground方法中:

    @Override
       protected List<IssueInfo> doInBackground(Void... params) {
            IssueCreator = new IssueInfoCreator(this, Integer.parseInt(AppPreferences.mDBVersion));
        IssueCreator.open();

        IssueCreator.close();
        return IssueCreator.queryAll();

   }

你的AsyncTask也应该是:

private class LoadIssuesTask extends AsyncTask<Void, Void, List<IssueInfo>>

答案 1 :(得分:-3)

private void LoadIssues方法调用handler.setMessage(0)中创建private Handler实例以调用setAdapter方法

使用Handler代替Asynctask