listview程序

时间:2015-09-08 04:10:31

标签: listview

我已经使用listview完成了一个程序..我得到了一个运行时异常。请帮助我。 这是我的logcat

09-08 09:02:46.924: D/AndroidRuntime(2319): Shutting down VM
09-08 09:02:46.924: D/AndroidRuntime(2319): --------- beginning of crash
09-08 09:02:46.949: E/AndroidRuntime(2319): FATAL EXCEPTION: main
09-08 09:02:46.949: E/AndroidRuntime(2319): Process: com.example.todo, PID: 2319
09-08 09:02:46.949: E/AndroidRuntime(2319): java.lang.RuntimeException:        Unable to start activity   ComponentInfo{com.example.todo/com.example.todo.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
09-08 09:02:46.949: E/AndroidRuntime(2319):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at android.os.Looper.loop(Looper.java:135)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at android.app.ActivityThread.main(ActivityThread.java:5221)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at java.lang.reflect.Method.invoke(Native Method)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at java.lang.reflect.Method.invoke(Method.java:372)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
09-08 09:02:46.949: E/AndroidRuntime(2319): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
09-08 09:02:46.949: E/AndroidRuntime(2319):     at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at android.widget.ListView.setAdapter(ListView.java:487)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at com.example.todo.MainActivity.onCreate(MainActivity.java:32)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at android.app.Activity.performCreate(Activity.java:5937)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
09-08 09:02:46.949: E/AndroidRuntime(2319):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
09-08 09:02:46.949: E/AndroidRuntime(2319):     ... 10 more
09-08 09:02:51.130: I/Process(2319): Sending signal. PID: 2319 SIG: 9
09-08 09:03:06.387: I/art(2342): Background sticky concurrent mark sweep GC freed 1740(104KB) AllocSpace objects, 0(0B) LOS objects, 18% free, 508KB/623KB, paused 1.661ms total 107.680ms
09-08 09:03:06.602: I/art(2342): Background partial concurrent mark sweep GC freed 903(101KB) AllocSpace objects, 0(0B) LOS objects, 42% free, 699KB/1211KB, paused 3.624ms total 204.798ms
09-08 09:03:06.607: D/AndroidRuntime(2342): Shutting down VM
09-08 09:03:06.608: E/AndroidRuntime(2342): FATAL EXCEPTION: main
09-08 09:03:06.608: E/AndroidRuntime(2342): Process: com.example.todo, PID: 2342
09-08 09:03:06.608: E/AndroidRuntime(2342): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todo/com.example.todo.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
09-08 09:03:06.608: E/AndroidRuntime(2342):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at android.os.Looper.loop(Looper.java:135)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at android.app.ActivityThread.main(ActivityThread.java:5221)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at java.lang.reflect.Method.invoke(Native Method)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at java.lang.reflect.Method.invoke(Method.java:372)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
09-08 09:03:06.608: E/AndroidRuntime(2342): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
09-08 09:03:06.608: E/AndroidRuntime(2342):     at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at android.widget.ListView.setAdapter(ListView.java:487)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at com.example.todo.MainActivity.onCreate(MainActivity.java:32)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at android.app.Activity.performCreate(Activity.java:5937)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
09-08 09:03:06.608: E/AndroidRuntime(2342):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
09-08 09:03:06.608: E/AndroidRuntime(2342):     ... 10 more

这是我的代码...... 公共类MainActivity扩展ActionBarActivity {

private ArrayList<String> arrayList;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    ListView listView=(ListView)findViewById(R.id.l1);
    final EditText et=(EditText)findViewById(R.id.et1);
    Button btn=(Button)findViewById(R.id.b1);

    adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,arrayList);
    listView.setAdapter(adapter);

    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String newitem=et.getText().toString();
            arrayList.add(newitem);
            adapter.notifyDataSetChanged();
            et.setText("");

        }
    });
}

这是xml版本

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#ff000f"
 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="com.example.todo.MainActivity" >


<Button
    android:id="@+id/b1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="add" />

<ListView
    android:id="@+id/l1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/b1"
    android:layout_alignRight="@+id/b1" >

</ListView>

<EditText
    android:id="@+id/et1"
    android:background="#ffffff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/b1"
    android:layout_alignBottom="@+id/b1"
    android:layout_alignLeft="@+id/l1"
    android:ems="10" />


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

你应该初始化你的arrayList。

      ArrayList<String> arrayList = null; 

您正在传递给适配器null