Scott
Scott

Reputation: 3732

Android: Getting an IllegalStateException that I cannot catch

I'm getting an error in my application. My usual debugging method is to track down the line causing the problem, wrap it up in Logs, and figure out what I did wrong. In this case, I believe I know which line is causing the problem, but it's stumping me.

Worse, I wrapped it in try/catch and nothing is being caught. So I conclude one of two things is going on:

So tell me, is there any chance the error is not caused by the setText line? And if not, why couldn't catch catch it? (BTW, clicking on the line 207 link brings me to the "try" line. The method this is in is called from an onClick method. R.id.updater_title refers to a TextView inside the inflater. mThis refers to "this" from )

Based on these lines of code:

    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
    }

And this Log stack:

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)

Upvotes: 0

Views: 1995

Answers (1)

codeMagic
codeMagic

Reputation: 44571

It looks like the problem is

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

should be

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

You need to use the findVeiwById() method on the View that you inflate which holds the TextVeiw you are looking for.

And as Jon Skeet commented, you don't want to wrap the bug in a try/catch, even if it was just to find the problem. You see in the logcat that it's a NPE so just go there and see what could be null then find out why it is null.

Upvotes: 1

Related Questions