私のアプリは CallLog を使用してデータを収集し、リストビューに入れます。これには時間がかかるため、progressdialog を使用してユーザーに読み込みのステータスを表示しようとしました。残念ながら(以前にprogressdialogを使用したことがあります)Eclipseはエラーをスローします:ERROR/AndroidRuntime(771):android.view.ViewRoot $ CalledFromWrongThreadException:ビュー階層を作成した元のスレッドのみがそのビューにアクセスできます。
public class Calls extends Activity {
//declaring variables
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calls);
lv1 = (ListView) findViewById(R.id.ListView01);
pd = new ProgressDialog(Calls.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("Loading contacts");
pd.show();
//Thread thread = new Thread() {
//public void run() {
Calls.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//collecting data from CallLog, putting data into an array
//Here comes the hard part, which is the root of the problem:
final ArrayList<SearchResults> results = new ArrayList<SearchResults>();
SearchResults sr1 = new SearchResults();
for (int b=0; b<storage.length; b++)
{
for (int e=0; e<storage[b].length; e++)
{
if (e+3 < storage[b].length)
{
arr_split_all.add(storage[b][e] + " " + storage[b][e+1] + " " + storage[b][e+2] + " " + storage[b][e+3]);
sr1 = new SearchResults();
sr1.setData1(storage[b][e+2]);
sr1.setData2(storage[b][e]);
sr1.setData3(storage[b][e+1]);
sr1.setData4(storage[b][e+3]);
sr1.setBitmap2(bitmaparray[b]); //bitmaparray has the same size as storage, and there is no problem with this
results.add(sr1);
}
}
}
lv1.setAdapter(new MyCustomBaseAdapter(Calls.this, results)); //problematic row
handler.sendEmptyMessage(0);
}
};
thread.start();
}
}//end of onCreate
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
pd.dismiss();
}
}); //ADDED a )
}//end of class
// SearchResults is another class:
public class SearchResults extends Application{
private String data1 = "";
private String data2 = "";
private String data3 = "";
private String data4 = "";
private String bitmap = "";
private Bitmap bitmap2;
public void setData1(String data1) {
this.data1 = data1;
}
public String getData1() {
return data1;
}
//etc...
}
また、BaseAdapter クラスを使用してデータをリストビュー (イメージビューと 4 つのテキストビューで構成される) の適切な場所に配置していますが、現在は関係ないと思います。
Logcat によると、問題は lv1.setAdapter(new MyCustomBaseAdapter(Calls.this, results));
行にあります。リストビューに配列リストを添付すると、正常に動作します。プログレスダイアログがなければ、すべてのテキストビューとリストビューの各行のイメージビューが正しくロードされます。