Bucky Bucky - 1 month ago 12
Android Question

Getting error in main activity on my basic calculator app

I am making a simple android calculator app. There is no compile time error. But when i try to run the app. I mean when i tap on any button the app crashed.

Java

package com.buckydroid.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView screen;
private String str1,str2,str3,result,str,sign;
private Double a,b;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen = (TextView)findViewById(R.id.textview);

}
private void onClick(View v){
Button button = (Button) v;
str += button.getText().toString();
screen.setText(str);
a = Double.parseDouble(str);
str = "";
}
private void onClickSigns(View v){
Button button = (Button) v;
sign = ((Button) v).getText().toString();
screen.setText(sign);
str="";
}
private void calculate(View v){
Button button = (Button) v;
str2 = screen.getText().toString();
b = Double.parseDouble(str2);
if (sign .equals("+")){
result = a+b+"";
}
else if (sign .equals("-")){
result = a-b+"";
}
else if (sign .equals("X")){
result = a*b+"";
}
else if (sign .equals("÷")){
result = a/b+"";
}
else{
result = "Something went wrong";
}
screen.setText(result);
}

}


Error Log

10-08 19:44:59.361 19449-19449/com.buckydroid.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.buckydroid.myapplication, PID: 19449
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


So if you need any other code then please comment

Thank you in advance..

Answer

Ok I got it after building in Android studio. The error is a lot bigger than what you pasted and I was able to track it down with the stack trace.

Basically you are trying to concatenate a string to a null with this line in your onClick method: str += button.getText().toString();

Since you can't do that you can fix the issue by replacing:

str += button.getText().toString();

With this:

str = button.getText().toString();

OR

Since this is a calculator app, you can initialize str in the onCreate method with str = ""; and then also remove str = ""; from the onClick method.

Full Code here:

public class MainActivity extends AppCompatActivity {
private TextView screen;
private String str1,str2,str3,result,str,sign;
private Double a,b;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    screen = (TextView)findViewById(R.id.textview);
    str = "";
}
public void onClick(View v){
    Button button = (Button) v;
    str += button.getText().toString();
    screen.setText(str);
    a = Double.parseDouble(str);
}
public void onClickSigns(View v){
    Button button = (Button) v;
    sign = ((Button) v).getText().toString();
    screen.setText(sign);
    str="";
}
public void calculate(View v){
    Button button = (Button) v;
    str2 = screen.getText().toString();
    b = Double.parseDouble(str2);
    if (sign .equals("+")){
        result = a+b+"";
    }
    else if (sign .equals("-")){
        result = a-b+"";
    }
    else if (sign .equals("X")){
        result = a*b+"";
    }
    else if (sign .equals("÷")){
        result = a/b+"";
    }
    else{
        result = "Something went wrong";
    }
    screen.setText(result);
}

}
Comments