如果此错误/问题过于简单,我道歉。我没有开发Android应用程序的经验,所以我试图通过其中一个codepath教程,特别是this one,我在第11页。
这是java:
package com.example.simpletodo;
package com.example.simpletodo;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TodoActivity extends Activity {
ArrayList<String> items;
ArrayAdapter<String> itemsAdapter;
ListView lvItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo);
lvItems = (ListView) findViewById(R.id.lvItems);
items.add("First Item");
items.add("Second Item");
itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.todo, menu);
return true;
}
}
这是XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TodoActivity" >
<ListView
android:id="@+id/lvItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/etNewITem"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
<EditText
android:id="@+id/etNewITem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lvItems"
android:layout_alignParentBottom="true"
android:ems="10"
android:hint="Enter new item" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnAddItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Add" />
</RelativeLayout>
应用程序似乎在java第22行lvItems.setAdapter(itemsAdapter);
任何建议表示赞赏。我是一个php人,所以我不习惯冗长的代码(学习)以及如何调试已编译的程序。谢谢!
来自logcat的错误消息:
02-06 16:29:31.563: W/dalvikvm(18897): threadid=1: thread exiting with uncaught exception (group=0x41d39700)
02-06 16:29:31.563: E/AndroidRuntime(18897): FATAL EXCEPTION: main
02-06 16:29:31.563: E/AndroidRuntime(18897): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simpletodo/com.example.simpletodo.TodoActivity}: java.lang.NullPointerException
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.access$700(ActivityThread.java:168)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.os.Looper.loop(Looper.java:137)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.main(ActivityThread.java:5493)
02-06 16:29:31.563: E/AndroidRuntime(18897): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 16:29:31.563: E/AndroidRuntime(18897): at java.lang.reflect.Method.invoke(Method.java:525)
02-06 16:29:31.563: E/AndroidRuntime(18897): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
02-06 16:29:31.563: E/AndroidRuntime(18897): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
02-06 16:29:31.563: E/AndroidRuntime(18897): at dalvik.system.NativeStart.main(Native Method)
02-06 16:29:31.563: E/AndroidRuntime(18897): Caused by: java.lang.NullPointerException
02-06 16:29:31.563: E/AndroidRuntime(18897): at com.example.simpletodo.TodoActivity.onCreate(TodoActivity.java:21)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.Activity.performCreate(Activity.java:5372)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
02-06 16:29:31.563: E/AndroidRuntime(18897): ... 11 more
答案 0 :(得分:6)
您永远不会初始化items变量,这会导致您在尝试设置适配器时获取空指针异常,因为items为null。
这样做:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo);
// initialize
items = new ArrayList<String>();
// add content
items.add("First Item");
items.add("Second Item");
lvItems = (ListView) findViewById(R.id.lvItems);
itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
}
您可以在logcat中看到错误:
...
Caused by: java.lang.NullPointerException 02-06 16:26:55.038: E/AndroidRuntime(17822): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330) 02-06 16:26:55.038: E/AndroidRuntime(17822): at android.widget.ListView.setAdapter(ListView.java:470) 02-06 16:26:55.038: E/AndroidRuntime(17822): at com.example.simpletodo.TodoActivity.onCreate(TodoActivity.java:22) 02-06 ....
这告诉您NullPointerException
发生在TodoActivity
第22行。
答案 1 :(得分:0)
在添加日志猫之前我们无法知道这个问题,但我确实发现了以下内容:
您可以在项目中包含任何内容之前将其添加到数组中,并按照此类
进行修复 items.add("First Item");
items.add("Second Item");
itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
答案 2 :(得分:-1)
您在ArrrayAdapter中的问题
items.add("First Item");
items.add("Second Item");
itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);