我有一个简单的应用程序,它总结了两个数字,结果出现在第二个活动中。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;
}}}
我的其余代码:
主要活动:
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>