Bhavesh
Bhavesh

Reputation: 137

App stopped working as soon as i clicked the button

I built a simple project that squares a given number. In that app as soon as i click the button "Calculate" it say's "Unfortunately, SquareCalculator has stopped". What should i do to resolve the problem?

Below is my entire code:-

MainActivity.java

package thenerdimite.squarecalculator;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    protected void calculate (View v){
        EditText input = (EditText) findViewById(R.id.etInput);
        TextView output = (TextView) findViewById(R.id.tvOutput);

        int base = Integer.valueOf(input.getText().toString());
        int result = base * base;
        String formattedResult = String.format("%,d", result);

        output.setText("Result: " + formattedResult);
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="thenerdimite.squarecalculator.MainActivity">

    <EditText
        android:id="@+id/etInput"
        android:layout_width="245dp"
        android:layout_height="41dp"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:inputType="number"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="62dp"
        android:layout_below="@+id/tvInput"
        android:layout_alignStart="@+id/tvInput" />

    <Button
        android:id="@+id/btnCalculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/etInput"
        android:layout_below="@+id/etInput"
        android:layout_marginTop="20dp"
        android:onClick="Calculate"
        android:text="Calculate"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tvOutput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result:"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:layout_below="@+id/btnCalculate"
        android:layout_alignStart="@+id/btnCalculate"
        android:layout_marginTop="18dp" />

    <TextView
        android:id="@+id/tvInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter an Integer:"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:layout_marginStart="19dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

logcat

04-25 09:28:05.060 3596-3596/thenerdimite.squarecalculator D/AndroidRuntime: Shutting down VM
04-25 09:28:05.060 3596-3596/thenerdimite.squarecalculator W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa5007678)
04-25 09:28:05.060 3596-3596/thenerdimite.squarecalculator E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             java.lang.IllegalStateException: Could not find method Calculate(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnCalculate'
                                                                                 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:4240)
                                                                                 at android.view.View$PerformClick.run(View.java:17721)
                                                                                 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:5103)
                                                                                 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:737)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                                 at dalvik.system.NativeStart.main(Native Method)
04-25 09:28:06.740 3596-3596/? I/Process: Sending signal. PID: 3596 SIG: 9

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="thenerdimite.squarecalculator">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Upvotes: 0

Views: 175

Answers (5)

Dmytro
Dmytro

Reputation: 2309

Change android:onClick="Calculate" to android:onClick="calculate". Also calculate method should be public.

Upvotes: 1

User1911
User1911

Reputation: 394

you need to make the method public, protected and private methods are not accessible by xml.

public void calculate (View v){
            EditText input = (EditText) findViewById(R.id.etInput);
            TextView output = (TextView) findViewById(R.id.tvOutput);

            int base = Integer.valueOf(input.getText().toString());
            int result = base * base;
            String formattedResult = String.format("%,d", result);

            output.setText("Result: " + formattedResult);
        }

Upvotes: 1

Loginus
Loginus

Reputation: 439

In XML you have "Calculate" with capital "C" while method is called "calculate". Change XML and it should work.

https://developer.android.com/reference/android/widget/Button.html

Upvotes: 0

AxelH
AxelH

Reputation: 14572

Using the documentation of Button

You see the method need to be public and accept a View

In order for this to work, the method must be public and accept a View as its only parameter.

So it should be

public void Calculate (View v){ 

Using the xml notation, of course, you should call it calculate and correct the XML

Upvotes: 1

Adriel Iclodean
Adriel Iclodean

Reputation: 80

From what I see, the problem is that your button action is android:onClick="Calculate" but should be android:onClick="calculate"

Upvotes: 1

Related Questions