我正在尝试设置一个执行一个任务的方法,如果我的应用程序已经运行过 - 而另一个任务,如果没有。
为此,我创建了一个String runPrevious
:
我的问题是每次我尝试执行应用程序时都会因为空指针异常而关闭力:
if (runPrevious.equals("yes")) {
我不明白 - 因为我在这里声明了String的值:
String runPrevious = "No";
12-13 13:20:12.485: E/AndroidRuntime(1803): FATAL EXCEPTION: main
12-13 13:20:12.485: E/AndroidRuntime(1803): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.idg.omv/com.idg.omv.ui.phone.Home}: java.lang.NullPointerException
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.access$600(ActivityThread.java:156)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.os.Looper.loop(Looper.java:153)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.main(ActivityThread.java:5297)
12-13 13:20:12.485: E/AndroidRuntime(1803): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 13:20:12.485: E/AndroidRuntime(1803): at java.lang.reflect.Method.invoke(Method.java:511)
12-13 13:20:12.485: E/AndroidRuntime(1803): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-13 13:20:12.485: E/AndroidRuntime(1803): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-13 13:20:12.485: E/AndroidRuntime(1803): at dalvik.system.NativeStart.main(Native Method)
12-13 13:20:12.485: E/AndroidRuntime(1803): Caused by: java.lang.NullPointerException
12-13 13:20:12.485: E/AndroidRuntime(1803): at com.idg.omv.ui.phone.Home.onCreate(Home.java:113)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.Activity.performCreate(Activity.java:5262)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
12-13 13:20:12.485: E/AndroidRuntime(1803): ... 11 more
12-13 13:20:13.969: I/Process(1803): Sending signal. PID: 1803 SIG: 9
public class Home extends YouTubeBaseActivity implements
VideoClickListener {
String runPrevious = "No";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
String runPrevious = getIntent().getStringExtra("runPrevious");
if (runPrevious.equals("yes")) {
String playlist = getIntent().getStringExtra("playlist");
new GetYouTubeUserVideosTask(responseHandler, playlist).execute();
}else{
new GetYouTubeUserVideosTask(responseHandler, playlist).execute();
}
答案 0 :(得分:1)
您再次声明它(可能是空值)
String runPrevious = getIntent().getStringExtra("runPrevious");
答案 1 :(得分:1)
如果要将字符串与字面值进行比较,请不要测试字符串为null。而是使用非空指针可成功的compairsons。
在你的情况下:
if ("yes".equals(runPrevious))
这将从不抛出空指针异常。