当我引用ArrayList.add方法时,Android应用程序在启动时崩溃

时间:2012-07-05 21:32:13

标签: java android crash nullpointerexception startup

以下是我的MainActivity的完整代码。如你所见,我只添加了2行:

ArrayList<String> y = null; 
y.add(new String("buggy")); // When I remove this line it works

当我注释掉y.add()时,应用程序不会崩溃。

我的问题是:为什么使用y.add()导致程序失败?

移动设备是三星Galaxy Mini S5570,运行Android 2.3.4

这是完整的代码,后面是LogCat:

package com.testbug;
import javax.*;
import java.util.*;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu; 
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //
    // Only these 2 lines added by me. The rest is automatically generated in Eclipse.
    ArrayList<String> y = null; 
    y.add(new String("buggy")); // When I remove this line it works
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

堆栈跟踪:

07-05 21:01:37.919: D/AndroidRuntime(32067): Shutting down VM
07-05 21:01:37.919: W/dalvikvm(32067): threadid=1: thread exiting with uncaught exception (group=0x40018578)
07-05 21:01:37.929: E/AndroidRuntime(32067): FATAL EXCEPTION: main
07-05 21:01:37.929: E/AndroidRuntime(32067): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testbug/com.testbug.MainActivity}: java.lang.NullPointerException
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.os.Looper.loop(Looper.java:130)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.ActivityThread.main(ActivityThread.java:3687)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at java.lang.reflect.Method.invokeNative(Native Method)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at java.lang.reflect.Method.invoke(Method.java:507)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at dalvik.system.NativeStart.main(Native Method)
07-05 21:01:37.929: E/AndroidRuntime(32067): Caused by: java.lang.NullPointerException
07-05 21:01:37.929: E/AndroidRuntime(32067):    at com.testbug.MainActivity.onCreate(MainActivity.java:20)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-05 21:01:37.929: E/AndroidRuntime(32067):    ... 11 more

2 个答案:

答案 0 :(得分:6)

您将其设置为null然后在其上调用方法...这将导致空指针

在调用add():

之前将其初始化为
ArrayList<String> y = new ArrayList<String>();

答案 1 :(得分:0)

您必须初始化声明的数组。如果您尝试将项目添加到空数组,则会导致java.lang.NullPointerException(正是您所拥有的)。

您必须执行以下操作:

//Declaring and Initializing
ArrayList<String> y = new ArrayList<String>();
    y.add(new String("buggy"));