gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:22:25 26 4
gpt4 key购买 nike

这是我的 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

最佳答案

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

在调用 add() 之前像这样初始化它:

ArrayList<String> y = new ArrayList<String>();

关于java - 当我引用 ArrayList.add 方法时,Android 应用程序在启动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11352869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com