2010-01-30 29 views
0

我正在嘗試編寫我的第一個Android應用程序,並且在填充數據庫中的列表佈局時遇到了一點點 問題。無法填充應用程序中的列表佈局

項目本身是在這裏:

http://code.google.com/p/biofuelsfinder/

但是我遇到問題的特定代碼是在這裏:

public void fillData(String fueltype){ 
     ArrayList<String> items = new ArrayList<String>(); 
     Cursor retailers = biofuelsDB.getRetailers("b5"); 
     /*while(!retailers.isAfterLast()){ 
       retailers.moveToNext(); 
       try{ 
        items.add(retailers.getString(retailers.getColumnIndex("name"))); 
       } catch(IllegalStateException e){ 
        String msg = e.getMessage(); 
       } 
       //items.add(user.lastName); 
       //items.add(user.country); 
     }*/ 

     items.add("Blah1"); 
     items.add("Blah2"); 
     items.add("Blah3"); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      R.layout.retailers_row, items); 
     /*setListAdapter(adapter);*/ 
} 

兩個部分註釋掉,如果運行的原因和illegalStateException 錯誤,然後中止代碼。

如果任何人都可以看看,並讓我知道我要去哪裏錯了, 將不勝感激。

下面是堆棧跟蹤

W/dalvikvm( 208): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
E/AndroidRuntime( 208): Uncaught handler: thread main exiting due to uncaught exception 
E/AndroidRuntime( 208): java.lang.IllegalStateException: Could not execute method of the activity 
E/AndroidRuntime( 208): at android.view.View$1.onClick(View.java:2031) 
E/AndroidRuntime( 208): at android.view.View.performClick(View.java:2364) 
E/AndroidRuntime( 208): at android.view.View.onTouchEvent(View.java:4179) 
E/AndroidRuntime( 208): at android.widget.TextView.onTouchEvent(TextView.java:6532) 
E/AndroidRuntime( 208): at android.view.View.dispatchTouchEvent(View.java:3709) 
E/AndroidRuntime( 208): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) 
E/AndroidRuntime( 208): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) 
E/AndroidRuntime( 208): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) 
E/AndroidRuntime( 208): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) 
E/AndroidRuntime( 208): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659) 
E/AndroidRuntime( 208): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 
E/AndroidRuntime( 208): at android.app.Activity.dispatchTouchEvent(Activity.java:2061) 
E/AndroidRuntime( 208): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 
E/AndroidRuntime( 208): at android.view.ViewRoot.handleMessage(ViewRoot.java:1690) 
E/AndroidRuntime( 208): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime( 208): at android.os.Looper.loop(Looper.java:123) 
E/AndroidRuntime( 208): at android.app.ActivityThread.main(ActivityThread.java:4310) 
E/AndroidRuntime( 208): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime( 208): at java.lang.reflect.Method.invoke(Method.java:521) 
E/AndroidRuntime( 208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
E/AndroidRuntime( 208): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
E/AndroidRuntime( 208): at dalvik.system.NativeStart.main(Native Method) 
E/AndroidRuntime( 208): Caused by: java.lang.reflect.InvocationTargetException 
E/AndroidRuntime( 208): at com.biofuelsfinder.biofuelsfinder.buttonClickHandler(biofuelsfinder.java:37) 
E/AndroidRuntime( 208): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime( 208): at java.lang.reflect.Method.invoke(Method.java:521) 
E/AndroidRuntime( 208): at android.view.View$1.onClick(View.java:2026) 
E/AndroidRuntime( 208): ... 21 more 
E/AndroidRuntime( 208): Caused by: java.lang.NullPointerException 
E/AndroidRuntime( 208): at com.biofuelsfinder.RetailerList.fillData(RetailerList.java:34) 
E/AndroidRuntime( 208): ... 25 more 

感謝

+2

首先,如果你得到一個例外,張貼堆棧跟蹤你的代碼一起,因爲它是很難爲你提供反饋沒有這個信息。其次,考慮使用'CursorAdapter'(例如'SimpleCursorAdapter'),而不是將「Cursor」中的東西拷貝到'ArrayAdapter'中。 – CommonsWare 2010-01-30 15:05:55

回答

0

加入這一行Arrayadapter後:

ListView lv; 
lv.setAdapter(items);