我最近编写了这个应用程序,用户将输入信息编辑到 edittext ,字符串将被添加到列表视图中。 待办事项列表
但是在项目的非常开始,当我第一次运行它时,应用程序崩溃了。
我正在使用教程来制作 here
我的代码如下所示。
public class MainActivity extends AppCompatActivity {
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvItems = (ListView) findViewById(R.id.lvItems);
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this, R.layout.listview, items);
lvItems.setAdapter(itemsAdapter);
items.add("First Item");
items.add("Second Item");
}
出于某种原因,当我输入特定代码时,一直在崩溃 ...
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this, R.layout.listview, items);
lvItems.setAdapter(itemsAdapter);
items.add("First Item");
items.add("Second Item");
这里是logcat输出......
11-21 20:25:35.478 12276-12276/? I/art: Late-enabling -Xcheck:jni
11-21 20:25:35.566 12276-12283/? I/art: Debugger is no longer active
11-21 20:25:35.566 12276-12283/? I/art: Starting a blocking GC Instrumentation
11-21 20:25:35.566 12276-12283/? I/art: Enter while loop.
11-21 20:25:35.594 12276-12276/? W/System: ClassLoader referenced unknown path: /data/app/com.dobleu.hotlists-1/lib/arm64
11-21 20:25:35.639 12276-12276/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-21 20:25:35.721 12276-12287/? I/art: Enter while loop.
11-21 20:25:35.785 12276-12276/? D/AndroidRuntime: Shutting down VM
11-21 20:25:35.787 12276-12276/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dobleu.hotlists, PID: 12276
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dobleu.hotlists/com.dobleu.hotlists.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2728)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6293)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1094)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.dobleu.hotlists.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:6729)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2681)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6293)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1094)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
11-21 20:25:37.413 12276-12283/com.dobleu.hotlists I/art: Enter while loop.
请有人告诉我错误