应用程序崩溃后退按钮(显式意图传递信息)

时间:2017-01-11 13:49:22

标签: android exception android-intent onclicklistener explicit-intent

我有简单的应用程序,它总结了两个数字,结果显示在第二个活动中。 Main Activity处理两个值和Sum按钮。按下Sum按钮后,第二个Activity将启动,显示结果并具有后退按钮。按下后退按钮后,应用程序崩溃,如果您按下Android设备提供的后退按钮,您将返回主要活动。 我也有分开的课程,每个班级都有自己的职能。

应用程序崩溃和原因是这些行:

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

活动,你可以找到这些行(ButtonListener-(照顾所有按钮)):

 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;
    }}}

我的其余代码:

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);
    }}

第二项活动(结果):

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);
    }
}

处理公式(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

2 个答案:

答案 0 :(得分:1)

将此代码替换为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;
            }
        }
    }

崩溃的原因:您接受来自ButtonListener构造函数的活动,并尝试初始化et1 = (EditText) activity.findViewById(R.id.ed1);

当您在MainActivity时,它可以正常工作,因为您将MainActivity上下文传递给它,并且您的编辑文本已初始化..

但是当你进入Result活动并且你传递了那个上下文时..想想..你可以用这个上下文初始化et1吗?

答案 1 :(得分:0)

感谢@Charuka的帮助!

我刚刚找到了正确答案,所有问题都是我确实声明了EditText并将String转换为Integer到了错误的地方,我必须在案例R.id.sum中进行。

要回到First Activity,您可以使用:

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

或:

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

两者都很好只有区别在于,在第一种情况下,您使用emty EditText获得MainActivity,而在第二种EditText中获得了previus值。 @Charuka感谢让我思考!