2012-10-23 77 views
-2

請幫我這個錯誤構造函數CursorAdapter(Context,Cursor,int)未定義..?

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) 
+0

你聲稱有一個編譯時錯誤,但它是一個運行時錯誤 – Blackbelt

+0

是的,它是一個運行時間error.can你讓我知道我錯了, – miku

+0

@Nunu回答給你。 – Blackbelt

回答

0

的問題是,你是一個非UI線程裏面UI操作..

您應該刪除此代碼從主線程,並在主線程中執行,或使用runOnUiThread(action)檢查http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable

+0

這些是兩個不同的類...從新線程開始是一個不同的類。錯誤是在線超(上下文,C,標誌);和customAdapter = new CustomAdapter(MyActivity.this,databaseHelper.getAllData(),CursorAdapter.NO_SELECTION); – miku

+0

在調用線程之前在活動中初始化適配器.. – Nermeen

+0

private CustomAdapter customAdapter;線程線程=新主題(){ \t \t \t公共無效的run(){ \t \t \t \t customAdapter =新CustomAdapter(MyActivity.this, \t \t \t \t \t \t databaseHelper.getAllData(),CursorAdapter.NO_SELECTION ); \t \t \t \t listView.setAdapter(customAdapter); \t \t \t} 我已alredy初始化該... – miku

0

嘗試t Ø在UI線程創建適配器:

puvblic class MyActivity extends Activity { 

    public void onCreate(...) { 
     customAdapter = new CustomAdapter(MyActivity.this, databaseHelper.getAllData(), CursorAdapter.NO_SELECTION); 
    } 
} 
+0

是的,我在我的UI線程創建只... – miku

+0

不,你不。您應該將您的適配器的創建移出新線程{...}部分 – Andy

+0

謝謝......它工作正常。 – miku

相關問題