2010-01-30 65 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);