Exorcist
Exorcist

Reputation: 252

Could not execute method of Activity

Well I have set of buttons and their onClick property is "click" like this android:onClick="click". I am running this in android 2.2

Here's the click function in the main activity. This function is outside the onCreate method as expected.

public void click(View v)
{
    String s = et2.getText().toString();

    switch(v.getId())
    {
        case R.id.b0 :      
            if(s.length() == 1 && s.charAt(0) == '0')
            {  
                et2.setText("0");
                et1.setText("0");
            }
            else
            {
                s = s + "0";
                call(s);
            }
        break;
        //Other cases
    }
}

The problem is whenever I click any button, the app stops working (Force Close).

Here's the Log:

03-30 22:23:20.129: E/AndroidRuntime(1240): FATAL EXCEPTION: main 03-30 22:23:20.129: E/AndroidRuntime(1240): java.lang.IllegalStateException: Could not execute method of the activity 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.view.View$1.onClick(View.java:3044) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.view.View.performClick(View.java:3511) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.view.View$PerformClick.run(View.java:14105) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.os.Handler.handleCallback(Handler.java:605) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.os.Handler.dispatchMessage(Handler.java:92) 03-30 22:23:20.129: E/AndroidRuntime(1240): at
android.os.Looper.loop(Looper.java:137) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.app.ActivityThread.main(ActivityThread.java:4424) 03-30 22:23:20.129: E/AndroidRuntime(1240): at java.lang.reflect.Method.invokeNative(Native Method) 03-30 22:23:20.129: E/AndroidRuntime(1240): at java.lang.reflect.Method.invoke(Method.java:511) 03-30 22:23:20.129: E/AndroidRuntime(1240): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 03-30 22:23:20.129: E/AndroidRuntime(1240): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 03-30 22:23:20.129: E/AndroidRuntime(1240): at dalvik.system.NativeStart.main(Native Method) 03-30 22:23:20.129: E/AndroidRuntime(1240): Caused by: java.lang.reflect.InvocationTargetException 03-30 22:23:20.129: E/AndroidRuntime(1240): at java.lang.reflect.Method.invokeNative(Native Method) 03-30 22:23:20.129: E/AndroidRuntime(1240): at java.lang.reflect.Method.invoke(Method.java:511) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.view.View$1.onClick(View.java:3039) 03-30 22:23:20.129: E/AndroidRuntime(1240): ... 11 more 03-30 22:23:20.129: E/AndroidRuntime(1240): Caused by: java.lang.NullPointerException 03-30 22:23:20.129: E/AndroidRuntime(1240): at yadav.sanjay.calculator.CalculatorActivity.click(CalculatorActivity.java:63)

Upvotes: 2

Views: 12364

Answers (3)

JXPheonix
JXPheonix

Reputation: 2668

Try changing the code on line 63 into two separate lines, like this.

String s; //you should probably change s to something less common like calS or the like
s = et2.getText().toString();

I think another thing that could be the problem is that et2 was never initialized. Make sure you have initialized et2.

Upvotes: 0

Tiago Almeida
Tiago Almeida

Reputation: 14237

Check if your et2 is inicialized. If you are using findViewById() please check if you do that after the setContent().

Upvotes: 0

antonio081014
antonio081014

Reputation: 3593

Caused by: java.lang.NullPointerException, Usually, this means your staff is not initialized yet. Check the code around Line 63.

Upvotes: 8

Related Questions