Log.d(String, String) 使我的程序崩溃?
Posted
技术标签:
【中文标题】Log.d(String, String) 使我的程序崩溃?【英文标题】:Log.d(String, String) crashes my program? 【发布时间】:2014-05-01 12:22:25 【问题描述】:如果我将此代码添加到我的任何方法中以检查片段是否为空;我的程序崩溃了。但是如果我注释掉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 ComponentInfocom.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
【问题讨论】:
我猜它的 if 条件实际上导致了崩溃,而不是 log d 可能是.. 那如果我只注释掉log.d而不注释掉任何条件,它会如何工作呢? if(cyclefragment.a1.equals(null)) 我发现了我的问题;我从未在 cyclefragment 类中实例化字段 a1。 请不要自行破坏您的帖子。没有问题,答案就变得毫无用处。 【参考方案1】:试试这个..
如果实际导致问题的条件不是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");
【讨论】:
【参考方案2】:我发现了我的问题;我从未在 cyclefragment 类中实例化字段 a1 。对不起各位。
【讨论】:
告诉你如果有问题!以上是关于Log.d(String, String) 使我的程序崩溃?的主要内容,如果未能解决你的问题,请参考以下文章