我在这里发现了类似的问题,但似乎没有一个解决方案有效。
这是我的自定义适配器的构造函数。
public ContactAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
{
super(context, layout, c, from, to,
}
这是应该填充列表视图的活动。
public class ContactList extends Activity {
private static final String KEY_ID = "_id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";
public static final String[] from = new String[] { KEY_ID, KEY_NAME,
KEY_PH_NO };
public static final int[] to = new int[] { R.id.textID, R.id.nameText,
R.id.numberText };
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_list);
DatabaseHandler db = new DatabaseHandler(this);
Cursor cur = db.getAllContacts();
ContactAdapter adapter = new ContactAdapter(this, R.layout.custom_list, cur, from, to);
listView.setAdapter(adapter);
}
它抛出一个空指针异常。这是我的堆栈树。
08-12 11:39:17.647: E/AndroidRuntime(6902): FATAL EXCEPTION: main
08-12 11:39:17.647: E/AndroidRuntime(6902): java.lang.RuntimeException: Unable to start activity ComponentInfo
{com.example.databaseproject/com.example.databaseproject.ContactList}: java.lang.NullPointerException
08-12 11:39:17.647: E/AndroidRuntime(6902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-12 11:39:17.647: E/AndroidRuntime(6902): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-12 11:39:17.647: E/AndroidRuntime(6902): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-12 11:39:17.647: E/AndroidRuntime(6902): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-12 11:39:17.647: E/AndroidRuntime(6902): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 11:39:17.647: E/AndroidRuntime(6902): at android.os.Looper.loop(Looper.java:137)
08-12 11:39:17.647: E/AndroidRuntime(6902): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-12 11:39:17.647: E/AndroidRuntime(6902): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 11:39:17.647: E/AndroidRuntime(6902): at java.lang.reflect.Method.invoke(Method.java:525)
08-12 11:39:17.647: E/AndroidRuntime(6902): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-12 11:39:17.647: E/AndroidRuntime(6902): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-12 11:39:17.647: E/AndroidRuntime(6902): at dalvik.system.NativeStart.main(Native Method)
08-12 11:39:17.647: E/AndroidRuntime(6902): Caused by: java.lang.NullPointerException
08-12 11:39:17.647: E/AndroidRuntime(6902): at com.example.databaseproject.ContactList.onCreate(ContactList.java:42)
08-12 11:39:17.647: E/AndroidRuntime(6902): at android.app.Activity.performCreate(Activity.java:5133)
08-12 11:39:17.647: E/AndroidRuntime(6902): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-12 11:39:17.647: E/AndroidRuntime(6902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-12 11:39:17.647: E/AndroidRuntime(6902): ... 11 more
请询问是否需要任何其他代码示例来跟踪问题。
PS:我检查了创建的数据库。它有值(它不是空的)。如果我评论这一行,它不会抛出错误。
listView.setAdapter(adapter);