Android:获取我无法捕获的IllegalStateException

时间:2014-05-16 12:41:49

标签: java android error-handling illegalstateexception

我的应用程序出错了。我通常的调试方法是跟踪引起问题的行,将其包装在日志中,并弄清楚我做错了什么。在这种情况下,我 相信 我知道导致问题的是哪一行,但它让我感到难过。

更糟糕的是,我把它包裹在try / catch中,没有任何东西被抓住。所以我总结了两件事之一:

  • 我错误地说哪一行导致错误
  • 我做了一些愚蠢的事情,它正盯着我看

那么告诉我,错误是否有可能不是由setText线引起的?如果没有,为什么不抓住它呢? (顺便说一句,点击207行链接将我带到"尝试"行。这个方法是从onClick方法调用的.R.id.updater_title指的是inflater里面的TextView。这是指到"这个"来自)

基于以下代码行:

    LayoutInflater layoutInflater = (LayoutInflater) mThis.getSystemService(LAYOUT_INFLATER_SERVICE);
    View view=layoutInflater.from(mThis).inflate(R.layout.connection_update,null);
    if (v.getId() == R.id.AddFriendButton) {   //Change dialog to Add
        Log.d("editUserDialog","updater_title: " + R.id.updater_title + "; AddTitle: " + R.string.Friends_AddTitle);
        Log.d("editUserDialog","AddTitle: " + getString(R.string.Friends_AddTitle));
        try {((TextView)findViewById(R.id.updater_title)).setText(R.string.Friends_AddTitle);}
        catch(Exception e) {Log.e("editUserDialog","Err: " + e.getMessage());}
        Log.d("editUserDialog - Add", "updater_title changed");
        Log.d("editUserDialog - Add","StateList1 = " + StateList.get(1));
        friendState.set(0,StateList.get(1),"your friend");   //"your friend" should ideally be replaces with friend's name
    }

这个日志堆栈:

05-16 00:34:26.295  17798-17798/com.farmsoft.lunchguru.app D/editUserDialog﹕ updater_title: 2131165293; AddTitle: 2131361798
        05-16 00:34:26.445  17798-17798/com.farmsoft.lunchguru.app D/editUserDialog﹕ AddTitle: Add New Friend
        05-16 00:34:26.455  17798-17798/com.farmsoft.lunchguru.app D/AndroidRuntime﹕ Shutting down VM
        05-16 00:34:26.455  17798-17798/com.farmsoft.lunchguru.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x417be898)
        05-16 00:34:26.485  17798-17798/com.farmsoft.lunchguru.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
        java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3852)
        at android.view.View.performClick(View.java:4489)
        at android.view.View$PerformClick.run(View.java:18803)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5455)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at android.view.View$1.onClick(View.java:3847)
        at android.view.View.performClick(View.java:4489)
        at android.view.View$PerformClick.run(View.java:18803)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5455)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at com.farmsoft.lunchguru.app.ManageFriends.editUserDialog(ManageFriends.java:207)
        at com.farmsoft.lunchguru.app.ManageFriends.onClick_AddFriend(ManageFriends.java:187)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at android.view.View$1.onClick(View.java:3847)
        at android.view.View.performClick(View.java:4489)
        at android.view.View$PerformClick.run(View.java:18803)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5455)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

看起来问题是

(TextView)findViewById(R.id.updater_title))

应该是

(TextView)view.findViewById(R.id.updater_title))

您需要在findVeiwById()上使用View方法,其中包含您要查找的TextVeiw。{/ p>

正如Jon Skeet评论的那样,你不想将错误包装在try/catch中,即使只是为了找到问题。你在logcat中看到它是NPE所以只需去那里看看可能null然后找出为什么它是{{1} }}