1

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

Charuක
  • 12,953
  • 5
  • 50
  • 88
lounchy
  • 76
  • 2
  • 11
  • When you post a question about a crash, you should also post the stack trace from your logcat. – Sunshinator Jan 11 '17 at 13:52
  • when you go back-- if the get string is empty then number1 in `int realNumber1 = Integer.parseInt(number1);` is a String which is empty so you try to cast a sing to int and it can crash – Charuක Jan 11 '17 at 13:57
  • Thanks, now i understand why it crashes. But how can i fix it? Any idea? I just add a LogCat with error messages. – lounchy Jan 11 '17 at 14:17
  • @lounchy check my answer – Charuක Jan 11 '17 at 15:07

2 Answers2

1

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 ?

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • Thank @Charuka, now it Works, but that is not so important, important is that i understand.Lets see if i understand. I was thinking that if you use finish(); it just closes Result activity and open the Last activity you been(This case Main Activity). In case if you have no EditText it Works like im saying. So how i understand, i was trying to finsh one activity, but actualy i had to start new activity. Is that right? – lounchy Jan 11 '17 at 15:47
  • @lounchy finishing is up to you.If you finish Activity it does not go to the back state so if you press back after that if there is not back state it ill close the app likewise. and also please note you need to handle your inputs for a int value if you accidentally add a string it will crash again .. – Charuක Jan 11 '17 at 16:21
0

Thank you @Charuka for your help!

I just found correct answer, all problema was that i did declare EditText and cast String to Integer into wrong place, i had to do it inside case R.id.sum.

And to get back to First Activity you can use:

case R.id.back:
                Intent i = new Intent(activity, MainActivity.class);
                activity.startActivity(i);
                break;

or:

case R.id.back:
                activity.finish();
                break;

Both are fine only diference is that in first case you get MainActivity with emty EditText´s and in second EditText´s has previus values. @Charuka thank for make me think!

lounchy
  • 76
  • 2
  • 11