0

I was recently coding this app where the user would input information to an edittext and the string would be added to the listview ie. a todo list

But at the very beginning of the project, when I first ran it, the app crashed.

I was using a tutorial to make it which can be found here

My code is shown below.

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");
}

For some reason, it keeps crashing when I input this particular piece of code...

items = new ArrayList<String>();
    itemsAdapter = new ArrayAdapter<String>(this, R.layout.listview, items);
    lvItems.setAdapter(itemsAdapter);
    items.add("First Item");
    items.add("Second Item");

Here's the logcat output...

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.

Please can someone show me what's wrong

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

0 Answers0