ListView和setAdapter出现了NullPointerException异常

5
我一直遇到以下的nullPointerException问题,但是我无法完全理解它,请帮助我解决。程序使用适配器从数据库查询数据并将其填充到ListView中。请帮忙检查一下。
   public class ViewListingsActivity extends Activity {
    SQLiteDatabase db;
    ListView listview;
    Button button;
    SimpleCursorAdapter adapter;
    private static final String TAG = DBHelper.class.getSimpleName();
    public static final String DB_ADDRESS = BaseColumns._ID;
    public static final String DB_DESCRIPTION = "Description";
    public static final String DB_URL = "URL";
    final String dbTable = "Realtor_SMS_Table";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewlisting);
        listview = (ListView) findViewById(R.id.list);
        button = (Button) findViewById(R.id.button1);
        DBHelper dbhelper = new DBHelper(ViewListingsActivity.this);
        db = dbhelper.getWritableDatabase();

        Cursor cursor = db.query(dbTable, null, null, null, null, null, null);
        startManagingCursor(cursor);

        String[] from = new String[] { DB_ADDRESS, DB_DESCRIPTION, DB_URL };
        int[] to = new int[] { R.id.textlistaddress, R.id.textlistdescription,
                R.id.textlisturl };
        adapter = new SimpleCursorAdapter(this, R.layout.rowlist, cursor, from,
                to);
        listview.setAdapter(adapter);

    }
}

这是我的堆栈跟踪信息

----- pid 764 at 2011-08-05 13:26:24 -----
Cmd line: com.RealtorSMS

DALVIK THREADS:
"main" prio=5 tid=3 NATIVE
  | group="main" sCount=1 dsCount=0 s=0 obj=0x40018e70
  | sysTid=764 nice=0 sched=0/0 handle=-1096475492
  at android.os.BinderProxy.transact(Native Method)
  at android.app.ActivityManagerProxy.handleApplicationError(ActivityManagerNative.java:2103)
  at com.android.internal.os.RuntimeInit.crash(RuntimeInit.java:302)
  at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:75)
  at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:887)
  at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:884)
  at dalvik.system.NativeStart.main(Native Method)

"Binder Thread #3" prio=5 tid=15 NATIVE
  | group="main" sCount=1 dsCount=0 s=0 obj=0x43594008
  | sysTid=771 nice=0 sched=0/0 handle=1505008
  at dalvik.system.NativeStart.run(Native Method)

"Binder Thread #2" prio=5 tid=13 NATIVE
  | group="main" sCount=1 dsCount=0 s=0 obj=0x43590120
  | sysTid=770 nice=0 sched=0/0 handle=1487504
  at dalvik.system.NativeStart.run(Native Method)

"Binder Thread #1" prio=5 tid=11 NATIVE
  | group="main" sCount=1 dsCount=0 s=0 obj=0x4358e9b0
  | sysTid=769 nice=0 sched=0/0 handle=1396928
  at dalvik.system.NativeStart.run(Native Method)

"JDWP" daemon prio=5 tid=9 VMWAIT
  | group="system" sCount=1 dsCount=0 s=0 obj=0x4358d2a0
  | sysTid=768 nice=0 sched=0/0 handle=1493656
  at dalvik.system.NativeStart.run(Native Method)

"Signal Catcher" daemon prio=5 tid=7 RUNNABLE
  | group="system" sCount=0 dsCount=0 s=0 obj=0x4358d1e8
  | sysTid=767 nice=0 sched=0/0 handle=1490832
  at dalvik.system.NativeStart.run(Native Method)

"HeapWorker" daemon prio=5 tid=5 VMWAIT
  | group="system" sCount=1 dsCount=0 s=0 obj=0x427d1928
  | sysTid=765 nice=0 sched=0/0 handle=1488192
  at dalvik.system.NativeStart.run(Native Method)

----- end 764 -----

以下是两个XML文件:
rowlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="match_parent" android:id="@+id/textlistaddress"></TextView>
    <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="match_parent" android:id="@+id/textlistdescription"></TextView>
    <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="match_parent" android:id="@+id/textlisturl"></TextView>

</LinearLayout>

viewlisting.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" android:background="@drawable/realtorbackground"
    android:weightSum="1">
    <TextView android:layout_weight="0.08" android:layout_height="wrap_content"
        android:layout_width="match_parent" android:id="@+id/textViewtitleview"
        android:gravity="center" android:textSize="30dp" android:text="Current Listings"></TextView>
    <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/list"></ListView>
    <Button android:id="@+id/button1" android:layout_width="match_parent"
        android:layout_height="wrap_content" android:text="Delete Listing"></Button>

</LinearLayout>

这是logcat:

08-05 13:26:24.077: ERROR/AndroidRuntime(764): Uncaught handler: thread main exiting due to uncaught exception
08-05 13:26:24.087: ERROR/AndroidRuntime(764): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.RealtorSMS/com.RealtorSMS.ViewListingsActivity}: java.lang.NullPointerException
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at android.os.Looper.loop(Looper.java:123)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at android.app.ActivityThread.main(ActivityThread.java:3948)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at java.lang.reflect.Method.invoke(Method.java:521)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at dalvik.system.NativeStart.main(Native Method)
08-05 13:26:24.087: ERROR/AndroidRuntime(764): Caused by: java.lang.NullPointerException
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at com.RealtorSMS.ViewListingsActivity.onCreate(ViewListingsActivity.java:39)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
08-05 13:26:24.087: ERROR/AndroidRuntime(764):     ... 11 more

3
请粘贴您的堆栈跟踪。 - azharb
根据设置,似乎有一些东西可能为空。哪一行代码抛出了空指针异常?能否提供堆栈跟踪信息? - Ashterothi
根据调试信息,它位于最后一行 "listview.setAdapter(adapter);"。 - Ryan
6个回答

17

请查看viewlisting.xml文件,它不包含id为R.id.list的ListView。


2
抱歉,我是指viewlisting.xml :) 因为如果在listview.setAdapter(adapter);行中出现NullPointerException,则意味着listview为空 而且它没有被找到 listview =(ListView)findViewById(R.id.list); - logcat
1
viewlisting.xml文件在ListView标签中没有正确的id,或者我漏掉了什么?这些xml已经被粘贴进去了。 - Ryan
1
奇怪,它们看起来没问题,你能从DDMS粘贴堆栈跟踪吗? - logcat
2
好的,经过疯狂地折腾了大约20分钟后,我决定从Activity更改为ListActivity,但这并没有起作用,所以我又改回去重新编译,关闭了Eclipse和AVD,重启后突然就可以了。你不觉得Eclipse有时候很容易出问题吗?我想知道为什么我一直在使用NetBeans进行其他Java工作。 - Ryan
1
谢谢!这也是我的问题。我太傻了,再次感谢! - M.Ionut
显示剩余10条评论

1
我曾经遇到过同样的问题。我发现自己使用了错误的参数在 setcontentview() 方法中,因为我是从另一个类中复制并粘贴了代码。我更正了错误的参数后,问题得到了解决!

1

我猜编译器或Eclipse出了问题。今晚我也遇到了这个问题。但是在我重命名了ListView ID之后,XML文件重新编译后就可以正常工作了!

我发现有时候XML文件的更改不会自动重新构建。有时候,XML文件的构建结果似乎也会丢失。


0

检查一下你在onCreate类方法中是否添加了这行代码:

setContentView(R.layout.your_xml);


0
Proapp Aplicativos的回答:“检查一下你是否在onCreate类方法中添加了这行代码:setContentView(R.layout.your_xml);”,帮助我解决了一个问题。

-1

在Eclipse项目中运行 -> 清除


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接