本文介绍了NullPointerException异常与ListView和setAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不断收到以下NullPointerException异常,我不能完全弄清楚任何帮助将大大AP preciated。该计划查询()的数据库,然后使用转接器来填充的ListView。
公共类ViewListingsActivity延伸活动{
SQLiteDatabase分贝;
ListView控件列表视图;
Button按钮;
SimpleCursorAdapter适配器;
私有静态最后字符串变量= DBHelper.class.getSimpleName();
公共静态最后弦乐DB_ADDRESS = BaseColumns._ID;
公共静态最后弦乐DB_DESCRIPTION =说明;
公共静态最后弦乐DB_URL =URL;
最后弦乐DBTABLE =Realtor_SMS_Table;
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.viewlisting);
列表视图=(ListView控件)findViewById(R.id.list);
按钮=(按钮)findViewById(R.id.button1);
DBHelper dbhelper =新DBHelper(ViewListingsActivity.this);
DB = dbhelper.getWritableDatabase();
光标光标= db.query(DBTABLE,NULL,NULL,NULL,NULL,NULL,NULL);
startManagingCursor(光标);
字符串从=新的String [] {DB_ADDRESS,DB_DESCRIPTION,DB_URL} [];
INT []到=新INT [] {R.id.textlistaddress,R.id.textlistdescription,
R.id.textlisturl};
适配器=新SimpleCursorAdapter(这一点,R.layout.rowlist,光标,从,
至);
listview.setAdapter(适配器);
}
}
这是我的堆栈跟踪
-----的pid 764在2011-08-05 13时26分24秒-----
CMD线:com.RealtorSMS
DALVIK主题:
主PRIO = 5 TID = 3 NATIVE
|组=主SCOUNT = 1 dsCount = 0 S = 0的obj = 0x40018e70
| sysTid = 764漂亮= 0 sched的= 0/0手柄= -1096475492
在android.os.BinderProxy.transact(本机方法)
在android.app.ActivityManagerProxy.handleApplicationError(ActivityManagerNative.java:2103)
在com.android.internal.os.RuntimeInit.crash(RuntimeInit.java:302)
在com.android.internal.os.RuntimeInit $ UncaughtHandler.uncaughtException(RuntimeInit.java:75)
在java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:887)
在java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:884)
在dalvik.system.NativeStart.main(本机方法)
捆扎线#3PRIO = 5 TID = 15 NATIVE
|组=主SCOUNT = 1 dsCount = 0 S = 0的obj = 0x43594008
| sysTid = 771漂亮= 0 sched的= 0/0手柄= 1505008
在dalvik.system.NativeStart.run(本机方法)
捆扎线2PRIO = 5 TID = 13 NATIVE
|组=主SCOUNT = 1 dsCount = 0 S = 0的obj = 0x43590120
| sysTid = 770漂亮= 0 sched的= 0/0手柄= 1487504
在dalvik.system.NativeStart.run(本机方法)
捆扎线#1PRIO = 5 TID = 11个地方
|组=主SCOUNT = 1 dsCount = 0 S = 0的obj = 0x4358e9b0
| sysTid = 769漂亮= 0 sched的= 0/0手柄= 1396928
在dalvik.system.NativeStart.run(本机方法)
JDWP守护PRIO = 5 TID = 9 VMWAIT
|组=系统SCOUNT = 1 dsCount = 0 S = 0的obj = 0x4358d2a0
| sysTid = 768漂亮= 0 sched的= 0/0手柄= 1493656
在dalvik.system.NativeStart.run(本机方法)
信号守望者守护PRIO = 5 TID = 7 RUNNABLE
|组=系统SCOUNT = 0 dsCount = 0 S = 0的obj = 0x4358d1e8
| sysTid = 767漂亮= 0 sched的= 0/0手柄= 1490832
在dalvik.system.NativeStart.run(本机方法)
HeapWorker守护PRIO = 5 TID = 5 VMWAIT
|组=系统SCOUNT = 1 dsCount = 0 S = 0的obj = 0x427d1928
| sysTid = 765漂亮= 0 sched的= 0/0手柄= 1488192
在dalvik.system.NativeStart.run(本机方法)
-----结束764 -----
下面是两个XMLS
rowlist.xml
< XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< TextView的Android版本:layout_height =WRAP_CONTENT机器人:文本=TextView的机器人:layout_width =match_parent机器人:ID =@ + ID / textlistaddress>< / TextView的>
< TextView的Android版本:layout_height =WRAP_CONTENT机器人:文本=TextView的机器人:layout_width =match_parent机器人:ID =@ + ID / textlistdescription>< / TextView的>
< TextView的Android版本:layout_height =WRAP_CONTENT机器人:文本=TextView的机器人:layout_width =match_parent机器人:ID =@ + ID / textlisturl>< / TextView的>
< / LinearLayout中>
viewlisting.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直机器人:layout_width =match_parent
机器人:layout_height =match_parent机器人:背景=@可绘制/ realtorbackground
机器人:weightSum =1>
< TextView的机器人:layout_weight =0.08机器人:layout_height =WRAP_CONTENT
机器人:layout_width =match_parent机器人:ID =@ + ID / textViewtitleview
机器人:重力=中心的Android版本:TEXTSIZE =30dp机器人:文本=当前列表>< / TextView的>
< ListView的机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT机器人:ID =@ + ID /列表>< / ListView控件>
<按钮机器人:ID =@ + ID / button1的机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT机器人:文本=删除列表>< /按钮>
< / LinearLayout中>
这里是logcat的:
08-05 13:26:24.077:ERROR / AndroidRuntime(764):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
08-05 13:26:24.087:ERROR / AndroidRuntime(764):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.RealtorSMS / com.RealtorSMS.ViewListingsActivity}:显示java.lang.NullPointerException
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在android.app.ActivityThread.access $ 1800(ActivityThread.java:112)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1692)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在android.os.Handler.dispatchMessage(Handler.java:99)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在android.os.Looper.loop(Looper.java:123)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在android.app.ActivityThread.main(ActivityThread.java:3948)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在java.lang.reflect.Method.invokeNative(本机方法)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在java.lang.reflect.Method.invoke(Method.java:521)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:782)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在dalvik.system.NativeStart.main(本机方法)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):由:显示java.lang.NullPointerException
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在com.RealtorSMS.ViewListingsActivity.onCreate(ViewListingsActivity.java:39)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
08-05 13:26:24.087:ERROR / AndroidRuntime(764):11 ...更多
解决方案
检查viewlisting.xml,它不包含ListView控件ID为R.id.list
I keep getting the following nullPointerException and I can't quite figure it out any help would be greatly appreciated. The program query() from DB and then use adapter to populate 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);
}
}
here is my stack trace
----- 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 -----
Here are the two xmls
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>
here is the 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
解决方案
check viewlisting.xml, It does not contain ListView with id R.id.list
这篇关于NullPointerException异常与ListView和setAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!