2012-03-12 5 views
1

私はActivityListViewを使用していますが、SQLite DBからロードするのに時間がかかるので、何かがロードされていることを知らせるためにProgressDialogを表示したかったのです。私は別のスレッドでタスクを実行しようとしましたが、私はCalledFromWrongThreadExceptionを取得しています。ListViewアダプタをAsyncThreadから設定できません

@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コード:IssueInfoAdapterため

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(); 
} 

コンストラクタ:ここに私のメインのActivityコードです

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

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

はそれはTHRです.setAdapterメソッドのエラーがLoadIssues()の中にあるためです。 ERROR:private void LoadIssuesメソッド呼び出しhandler.setMessage(0)

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. 

答えて

9

UIスレッドで実行されないdoInBackgroundメソッドのビューにアクセスしようとしています。また、あなたのAsyncTaskのあるべき

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

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

    } 

を::

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

@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方法で:あなたはUIスレッド上で動作する方法onPostExecuteにアダプタを設定する必要があります

-3

、代わりにAsynctasksetAdapter方法

使用Handlerを呼び出すためにprivate Handlerインスタンスを作成します。

関連する問題