由于某些原因,数据没有从第三个活动发送到主活动,应用程序崩溃,而第二个活动运行得很好


package com.example.intent;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText etEnterName;
Button btnActivity2;
Button btnActivity3;
TextView tvMessage;
final int ACTIVITY3=3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etEnterName=findViewById(R.id.etEnterName);
btnActivity2=findViewById(R.id.btnActivity2);
btnActivity3=findViewById(R.id.btnActivity3);
btnActivity2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (etEnterName.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "please enter all fields", Toast.LENGTH_SHORT).show();
}
else {
String name = etEnterName.getText().toString().trim();
Intent intent = new Intent(MainActivity.this,
com.example.intent.Activity2.class);
intent.putExtra("name",name);
startActivity(intent);
}
}
});
btnActivity3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
com.example.intent.Activity3.class);
startActivityForResult(intent,ACTIVITY3);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==ACTIVITY3)
{
if (resultCode==RESULT_OK){
tvMessage.setText(data.getStringExtra("surname"));
}
if(resultCode==RESULT_CANCELED){
tvMessage.setText("no data received");
}
}
}
}

第三次活动

package com.example.intent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Activity3 extends AppCompatActivity {
Button btnSubmit;
EditText etSurname;
Button btnCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3);
btnSubmit=findViewById(R.id.btnSubmit);
etSurname=findViewById(R.id.etSurname);
btnCancel=findViewById(R.id.btnCancel);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(etSurname.getText().toString().isEmpty()){
Toast.makeText(Activity3.this,"please enter your surname" Toast.LENGTH_SHORT).show();
}
else {
String surname = etSurname.getText().toString().trim();
Intent intent = new Intent();
intent.putExtra("surname",surname);
setResult(RESULT_OK,intent);
Activity3.this.finish();
}
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_CANCELED);
Activity3.this.finish();
}
});
}
}

这是清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intent">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activity3"></activity>
<activity android:name=".Activity2" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

解释

区块报价

我在关注一段youtube视频,但我找不到哪里出了问题。按钮主活动运行得很好,并将我带到第三个意图,但当我在编辑文本框中输入数据并单击提交按钮时,应用程序崩溃

我没有添加这行代码。tvMessage=findviewbyid(R.id.tvMessage(;我的坏——

相关内容

  • 没有找到相关文章