gpt4 book ai didi

android - 应用程序崩溃按下后退按钮(显式 Intent 传递信息)

转载 作者:行者123 更新时间:2023-11-30 00:47:15 25 4
gpt4 key购买 nike

我有一个简单的应用程序,它对两个数字求和,结果出现在第二个 Activity 中。 Main Activity 负责两个值和 Sum 按钮。当按下 Sum 按钮时,第二个 Activity 启动,显示结果并有后退按钮。一旦按下后退按钮,应用程序就会崩溃,如果您按下 Android 设备提供的后退按钮,您将返回到主 Activity 。我也有分开的类(class),每个类(class)都有自己的功能。

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

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

您可以在其中找到这些行的 Activity (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;
}}}

我的其余代码:

主要 Activity :

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

第二个 Activity (结果):

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 (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

最佳答案

将此代码替换为您的 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 构造函数中接受了一个 Activity,然后尝试初始化 et1 = (EditText) activity.findViewById(R.id. ed1);

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

但是当您在 Result Activity 中并传递该上下文时..想想..您可以使用该上下文初始化 et1 吗?

关于android - 应用程序崩溃按下后退按钮(显式 Intent 传递信息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41592686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com