如果我将此代码添加到我的任何方法中以检查片段是否为null;我的程序崩溃了。但是,如果我注释掉Log.d(),程序不会崩溃吗?
这里可能有什么问题?
if(cyclefragment.equals(null)){
Log.d("TESTING CYCLEFRAG NULL", "CycleFrag is null");
}else{
Log.d("TESTING CYCLEFRAG NULL", "CycleFrag is not null");
}
if(cyclefragment.a1.equals(null)){
Log.d("TESTING A1 NULL", "A1 is null");
}else{
Log.d("TESTING A1 NULL", "A1 is not null");
}
logcat的:
03-24 03:37:27.461: E/Trace(13979): error opening trace file: No such file or directory (2)
03-24 03:37:27.928: E/AndroidRuntime(13979): FATAL EXCEPTION: main
03-24 03:37:27.928: E/AndroidRuntime(13979): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nanospark.upcdemo/com.nanospark.upcdemo.MainActivity}: java.lang.NullPointerException
03-24 03:37:27.928: E/AndroidRuntime(13979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
03-24 03:37:27.928: E/AndroidRuntime(13979): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
03-24 03:37:27.928: E/AndroidRuntime(13979): at android.app.ActivityThread.access$600(ActivityThread.java:149)
03-24 03:37:27.928: E/AndroidRuntime(13979): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
03-24 03:37:27.928: E/AndroidRuntime(13979): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 03:37:27.928: E/AndroidRuntime(13979): at android.os.Looper.loop(Looper.java:153)
03-24 03:37:27.928: E/AndroidRuntime(13979): at android.app.ActivityThread.main(ActivityThread.java:4987)
03-24 03:37:27.928: E/AndroidRuntime(13979): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 03:37:27.928: E/AndroidRuntime(13979): at java.lang.reflect.Method.invoke(Method.java:511)
03-24 03:37:27.928: E/AndroidRuntime(13979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
03-24 03:37:27.928: E/AndroidRuntime(13979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-24 03:37:27.928: E/AndroidRuntime(13979): at dalvik.system.NativeStart.main(Native Method)
03-24 03:37:27.928: E/AndroidRuntime(13979): Caused by: java.lang.NullPointerException
03-24 03:37:27.928: E/AndroidRuntime(13979): at com.nanospark.upcdemo.MainActivity.onCreate(MainActivity.java:75)
03-24 03:37:27.928: E/AndroidRuntime(13979): at android.app.Activity.performCreate(Activity.java:5020)
03-24 03:37:27.928: E/AndroidRuntime(13979): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-24 03:37:27.928: E/AndroidRuntime(13979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
03-24 03:37:27.928: E/AndroidRuntime(13979): ... 11 more
答案 0 :(得分:3)
试试这个..
如果实际导致问题的条件不是log.d()
if(cyclefragment == null){
Log.d("TESTING CYCLEFRAG NULL", "CycleFrag is null");
}else{
Log.d("TESTING CYCLEFRAG NULL", "CycleFrag is not null");
}
if(cyclefragment.a1 == null){
Log.d("TESTING A1 NULL", "A1 is null");
}else{
Log.d("TESTING A1 NULL", "A1 is not null");
}
答案 1 :(得分:0)
我发现了我的问题;我从未在cyclefragment类中实例化字段a1。对不起伙计们。