我在Android中显示一个简单列表时遇到问题。
这是我的代码:
protected void updateList() {
final Activity a = this;
runOnUiThread(new Runnable() {
public void run() {
final ListView lv = (ListView) findViewById(R.id.sessions);
final Object[] array = sessionStore.toArray();
final String[] lst = new String[array.length];
for (int i = 0; i < array.length; i++) {
lst[i] = array[i].toString();
}
System.out.println("LIST = " + Arrays.toString(lst));
lv.setAdapter(new ArrayAdapter(a, android.R.layout.simple_list_item_1, R.id.listTextLayout, lst));
}
});
}
这是我的输出:
LIST = [ZoppedSession@0(id=79, queue=[])]
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x4190fda0)
FATAL EXCEPTION: main
Process: de.e_nexus.android.zopp.server, PID: 20124
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2724)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
at android.widget.ListView.onMeasure(ListView.java:1186)
at android.view.View.measure(View.java:17479)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17479)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17479)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17479)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548)
at android.view.View.measure(View.java:17479)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2285)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1396)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1595)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6630)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:573)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
我仔细阅读了这些问题
但是没有它们有答案,因为列表已填充并且适配器构造正确。
知道如何摆脱NPE吗?
答案 0 :(得分:1)
new ArrayAdapter(a, android.R.layout.simple_list_item_1, R.id.listTextLayout, lst)
请交换第二个和第三个参数。
第二个应该是布局,第三个应该是textview
答案 1 :(得分:1)
尝试删除R.id.listTextLayout
并调用3参数构造函数
new ArrayAdapter(a, android.R.layout.simple_list_item_1, list)