我的应用程序使用 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 类将数据放入列表视图的适当位置(由一个图像视图和四个文本视图组成),但我认为现在不相关。
根据 Logcat 的说法,问题出 lv1.setAdapter(new MyCustomBaseAdapter(Calls.this, results));
在行上。如果我将一个数组列表附加到列表视图,它工作正常。如果没有progressdialog,所有文本视图和列表视图每一行的图像视图都会正确加载。