Search code examples
androidcalculatorandroid-instant-run

"Unfortunately,<App name> has stopped working" Pops up whenever i click a button


I am trying to make a simple calculator using android studio. However when i click the button with text "1", the app crashes and the button with text "2" doesn't do anything. I have rightly added android:Onclick = "" to the xml. Please have a look at different source code files.

**PS: For now, I am trying to make the first two buttons work **

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.meenakshi.calculator.MainActivity">

<GridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_text"
        android:id="@+id/button"
        android:layout_row="0"
        android:layout_column="0"
        android:textColor="@color/abc_search_url_text_pressed"
        android:nestedScrollingEnabled="false"
        android:onClick="press1" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button16"
        android:layout_row="0"
        android:layout_column="3"
        android:text="@string/buttond_text" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button4_text"
        android:id="@+id/button4"
        android:layout_row="1"
        android:layout_column="0" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text= "@string/button2_text"
        android:id="@+id/button2"
        android:layout_row="0"
        android:layout_column="1"
        android:onClick="press2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button3_text"
        android:id="@+id/button3"
        android:layout_row="0"
        android:layout_column="2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buttonmul_text"
        android:id="@+id/button15"
        android:layout_row="1"
        android:layout_column="3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button7_text"
        android:id="@+id/button7"
        android:layout_row="2"
        android:layout_column="0" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button5_text"
        android:id="@+id/button5"
        android:layout_row="1"
        android:layout_column="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button6_text"
        android:id="@+id/button6"
        android:layout_row="1"
        android:layout_column="2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button8_text"
        android:id="@+id/button8"
        android:layout_row="2"
        android:layout_column="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buttonm_text"
        android:id="@+id/button14"
        android:layout_row="2"
        android:layout_column="3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button._text"
        android:id="@+id/button12"
        android:layout_row="3"
        android:layout_column="0" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button0_text"
        android:id="@+id/button10"
        android:layout_row="3"
        android:layout_column="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button9_text"
        android:id="@+id/button9"
        android:layout_row="2"
        android:layout_column="2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buttone_text"
        android:id="@+id/button11"
        android:layout_row="3"
        android:layout_column="2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buttonp_text"
        android:id="@+id/button13"
        android:layout_row="3"
        android:layout_column="3" />
</GridLayout>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/result"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="32dp"
    android:inputType="text" />

</RelativeLayout>

MainActivity.java

package com.example.meenakshi.calculator;


import android.content.DialogInterface; 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.view.View;
import android.widget.Button;



public class MainActivity extends AppCompatActivity {

Button btn = (Button)findViewById(R.id.button);
EditText edittext = (EditText)findViewById(R.id.result);
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void press1(View v){

    edittext.setText("1");
}

public void press2(View v){

        edittext.setText("2");
  }





} 

AndroidManifest.xml

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

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

Stack trace

   java.lang.IllegalStateException: Could not find method press(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
                                                                                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:4438)
                                                                                at android.view.View$PerformClick.run(View.java:18422)
                                                                                at android.os.Handler.handleCallback(Handler.java:733)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:136)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                                at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                                at dalvik.system.NativeStart.main(Native Method)

Solution

  • The problem here is that Activity has lifecycle and here we have following sequence of events:

    • initialisation of fields <- here you initialised Button and EditText(at this moment there is no layout, so findViewById returns null)
    • onCreate() <- here you inflating layout (after setContentView() you can find your views in layout)

    Some recommendations:

    Do not use onClick in xml - it makes it not obvious that something listens to onClick Event and in often (this case too) it is the root of problems. Use findViewById(R.id.button).setOnClickListener(); it makes that obvious.

    onCreate() should be the method to initialise everything in your activity

    UPDATE

    In the end you would have something like:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
            private EditText result;
            @Override
            protected void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                result = (EditText) findViewById(R.id.result);
                findViewById(R.id.button1).setOnClickListener(this);
                findViewById(R.id.button2).setOnClickListener(this);
            }
    
            @Override
            public void onClick(View v) {
                Button clickedBtn = (Button) v;
                switch (v.getId()){
                    case (R.id.button1):
                    case (R.id.button2):
                        result.append(clickedBtn.getText());
                        break;
                    default:
                        //some other
                        break;
    
                }
            }
        }
    

    Note: it is better NOT to put numbers and signs to strings.xml - despite AS complains about that, but you will not reuse that in your program and there is no case about translating those strings :)