2
votes

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)

3
click() should be onClick(). - Eng.Fouad
what is on line 63 of your CalculatorActivity.java ? - Win Myo Htet
Line 63 : String s=et2.getText().toString(); - Exorcist

3 Answers

8
votes

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

0
votes

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

0
votes

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.