androidexceptionandroid-intentonclicklistenerexplicit-intent

Aplication crashes pressing back button (Explicit Intent passing information)


I have simple app which sum up two numbers and result appears in Second Activity. The Main Activity takes care of two values and Sum button. When the Sum button is pressed, the second Activity launches, shows the result and has back button. Once the back button is pressed app crashes, if you press the Back Button provided by Android devices you get back to Main Activity. Also i have separated classes, every class has her own functions.

App crashes and reason are these lines:

String number1 = et1.getText().toString();
String number2 = et2.getText().toString();

Activity where you can find these lines(ButtonListener-(Takes care of all buttons)):

 package com.example.lounchy.explicitintent;

    import android.content.Intent;
    import android.view.View;
    import android.widget.EditText;

        public class ButtonListener extends MainActivity implements View.OnClickListener {
        MainActivity activity;
        EditText et1;
        EditText et2;

        public ButtonListener(MainActivity activity) {
            this.activity = activity;
        }

        @Override
        public void onClick(View view) {
            et1 = (EditText)activity.findViewById(R.id.nr1);
            et2 = (EditText)activity.findViewById(R.id.nr2);

            String number1 = et1.getText().toString();
            String number2 = et2.getText().toString();

            int realNumber1 = Integer.parseInt(number1);
            int realNumber2 = Integer.parseInt(number2);

            String resultToShow = StringUtil.sumUp(realNumber1, realNumber2);
            switch (view.getId()){
                case R.id.sum:
                    Intent intent = new Intent(activity, Result.class);
                    intent.putExtra("ready", resultToShow);
                    activity.startActivity(intent);
                    break;
                    case R.id.back:
                        finish();
                        break;
    }}}

Rest of My code:

MainActivity:

package com.example.lounchy.explicitintent;

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

public class MainActivity extends AppCompatActivity {

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

        ButtonListener buttonListener = new ButtonListener(this);

        Button sumUp = (Button)findViewById(R.id.sum);
        sumUp.setOnClickListener(buttonListener);
    }}

Second Activity (Result):

package com.example.lounchy.explicitintent;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

   public class Result extends MainActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        TextView textView = (TextView)findViewById(R.id.result);

        Intent intent = getIntent();

        String iResult = intent.getStringExtra("ready");
        textView.setText(iResult);

        ButtonListener buttonListener = new ButtonListener(this);

        Button back = (Button)findViewById(R.id.back);
        back.setOnClickListener(buttonListener);
    }
}

Activity which take care of formula(StringUtil):

package com.example.lounchy.explicitintent;

public class StringUtil extends MainActivity {

    public static String sumUp(int nr1, int nr2){
        int sum = nr1 + nr2;
        String result = Integer.toString(sum);
        return result;
    }
}

AndroidManifest.xml:

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

    <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>
        <activity android:name=".Result"/>

    </application>

</manifest>

enter image description here


Solution

  • Replace this code with your ButtonListener

    public class ButtonListener extends MainActivity implements View.OnClickListener {
            MainActivity activity;
            EditText et1;
            EditText et2;
    
            public ButtonListener(MainActivity activity) {
                this.activity = activity;
            }
    
            @Override
            public void onClick(View view) {
    
                switch (view.getId()) {
                    case R.id.sum:
    
                        et1 = (EditText) activity.findViewById(R.id.ed1);
                        et2 = (EditText) activity.findViewById(R.id.ed2);
    
                        String number1 = et1.getText().toString();
                        String number2 = et2.getText().toString();
    
                        int realNumber1 = Integer.parseInt(number1);
                        int realNumber2 = Integer.parseInt(number2);
    
                        String resultToShow = StringUtil.sumUp(realNumber1, realNumber2);
    
                        Intent intent = new Intent(activity, Result.class);
                        intent.putExtra("ready", resultToShow);
                        activity.startActivity(intent);
                        break;
                    case R.id.back:
                        Intent i = new Intent(activity, MainActivity.class);
                        activity.startActivity(i);
                        break;
                }
            }
        }
    

    Why it crash : You accept an Activity from ButtonListener constructor and there you try to initialize et1 = (EditText) activity.findViewById(R.id.ed1);

    It works fine when you are in MainActivity because you pass MainActivity context to it and your Edit texts get initialized..

    But when you are in Result activity and you pass that context.. Think ..can you initialize et1 with that context ?