简体   繁体   中英

Android app is crashing. Not sure why

I apologize if this error/issue is too trivial. I have zero experience developing android apps so I am attempting to go through one of the codepath tutorials, specifically this one , I'm at slide 11.

Here is the 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;
    }

}

Here is the 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>

The app appears to be crashing at java line #22 lvItems.setAdapter(itemsAdapter);

Any advice is appreciated. I'm a php guy, so I'm not used to verbose code (learning) and how to debug a compiled program. Thanks!

Error messages from 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

You never initialize the items variable, this causes you to get a null pointer exception when you try to set the adapter up because items is null.

do this:

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

You can see the error here in your 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 ....

This tells you that at a NullPointerException occurred at TodoActivity line 22.

We cant know the issue until you add your log cat, but I did spot the following:

You add your items to the array before there's anything in them, fix it like this

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

Your problem in 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM