-2

请帮我解决这个错误

public class CustomAdapter extends CursorAdapter {

public CustomAdapter(Context context,Cursor c,int flags) {
    **super(context,c,flags);** EEROR ON THIS LINE

}

Thread thread = new Thread() {

        public void run() {

            customAdapter = new CustomAdapter(MyActivity.this, databaseHelper.getAllData(), CursorAdapter.NO_SELECTION);

            listView.setAdapter(customAdapter);
        }

日志:

10-23 12:37:14.617: E/AndroidRuntime(668): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-23 12:37:14.617: E/AndroidRuntime(668):  at android.os.Handler.<init>(Handler.java:121)
10-23 12:37:14.617: E/AndroidRuntime(668):  at android.widget.CursorAdapter$ChangeObserver.<init>(CursorAdapter.java:449)
10-23 12:37:14.617: E/AndroidRuntime(668):  at android.widget.CursorAdapter.init(CursorAdapter.java:170)
10-23 12:37:14.617: E/AndroidRuntime(668):  at android.widget.CursorAdapter.<init>(CursorAdapter.java:116)
10-23 12:37:14.617: E/AndroidRuntime(668):  at com.example.databasesim.CustomAdapter.<init>(CustomAdapter.java:18)
10-23 12:37:14.617: E/AndroidRuntime(668):  at com.example.databasesim.MyActivity$1.run(MyActivity.java:39)
4

2 回答 2

0

问题是您正在非 ui 线程内进行 UI 操作。

您应该从线程中删除此代码,并在主线程中执行,或使用runOnUiThread(action)检查http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable )

于 2012-10-23T07:25:35.200 回答
0

尝试在 UI 线程中创建您的适配器:

puvblic class MyActivity extends Activity {

    public void onCreate(...) {
       customAdapter = new CustomAdapter(MyActivity.this, databaseHelper.getAllData(), CursorAdapter.NO_SELECTION);
    }
}
于 2012-10-23T07:25:48.960 回答