提问人:Megan 提问时间:9/18/2023 更新时间:9/18/2023 访问量:27
尝试使用 Intent 和 onClickListener 共享数据时,Android Studio 中的应用程序在单击提交时不断崩溃
App in Android Studio keeps crashing upon clicking submit when trying to share data using Intent and onClickListener
问:
该应用程序是一个通用的配置文件应用程序,用于熟悉该程序,因为这是我第一次使用它,而且我对 Java 没有太多经验。主活动有两个单选按钮、一个单选组、六个复选框、五个文本视图(三个标签和两个显示结果)和三个编辑文本。单击“应用”单选按钮中的结果时,复选框将显示分配给它们的文本视图。单击提交应该移动到下一个屏幕,该屏幕显示键入到 EditTexts 中并使用复选框和单选按钮选择的信息。
Main 活动 java 文件:
public class MainActivity extends AppCompatActivity {
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
applyButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
if (cookingbox.isChecked()) {
selectedhobby.setText(selectedhobby.getText().toString() + " " + cookingbox.getText().toString());
}
if (sportsbox.isChecked()) {
selectedhobby.setText(selectedhobby.getText().toString() + " " + sportsbox.getText().toString());
}
if (craftbox.isChecked()) {
selectedhobby.setText(selectedhobby.getText().toString() + " " + craftbox.getText().toString());
}
if (gamebox.isChecked()) {
selectedhobby.setText(selectedhobby.getText().toString() + " " + gamebox.getText().toString());
}
if (bookbox.isChecked()) {
selectedhobby.setText(selectedhobby.getText().toString() + " " + bookbox.getText().toString());
}
if (otherbox.isChecked()) {
selectedhobby.setText(selectedhobby.getText().toString() + " " + otherbox.getText().toString());
}
if (malebutton.isChecked()) {
gendertext.setText(malebutton.getText().toString());
} else if (femalebutton.isChecked()) {
gendertext.setText(femalebutton.getText().toString());
}
}
});
submitButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent Intent;
Intent = new Intent(MainActivity.this, Screen2Activity.class);
Intent.putExtra("selectedhobby", "");
Intent.putExtra("editTextText2", "");
Intent.putExtra("editTextText4", "");
Intent.putExtra("editTextText3", "");
Intent.putExtra("gendertext", "");
startActivity(Intent);
setContentView(R.layout.screen2);
}
});
}
}
Screen2 Java 文件:
import androidx.appcompat.app.AppCompatActivity;
public class Screen2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
;
textName = findViewById(R.id.textName);
textAge = findViewById(R.id.textAge);
textBirthplace = findViewById(R.id.textBirthplace);
textHobbies = findViewById(R.id.textHobbies);
textGender = findViewById(R.id.textGender);
Intent Intent = getIntent();
if (Intent != null)
{
String editTextText2 = Intent.getStringExtra("editTextText2");
String editTextText4 = Intent.getStringExtra("editTextText4");
String selectedhobby = Intent.getStringExtra("selectedhobby");
String gendertext = Intent.getStringExtra("gendertext");
Integer editTextText3 = Intent.getIntExtra("editTextText3", -1);
textName.setText(Log.d("Screen2Activity", "Name : + editTextText2"));
textAge.setText(Log.d("Screen2Activity", "Age : + editTextText3"));
textBirthplace.setText(Log.d("Screen2Activity", "Birthplace : + editTextText4"));
textHobbies.setText(Log.d("Screen2Activity", "Hobbies : + selectedhobby"));
textGender.setText(Log.d("Screen2Activity", "Gender : + gendertext"));
}
我无法从 EditText 字段获取信息以移动到第二个活动屏幕。当我回到主活动 java 文件尝试修复意图错误时,我想我也搞砸了提交按钮的代码,现在两者都不起作用。
答:
1赞
CommonsWare
9/18/2023
#1
首先更换:
textName.setText(Log.d("Screen2Activity", "Name : + editTextText2"));
textAge.setText(Log.d("Screen2Activity", "Age : + editTextText3"));
textBirthplace.setText(Log.d("Screen2Activity", "Birthplace : + editTextText4"));
textHobbies.setText(Log.d("Screen2Activity", "Hobbies : + selectedhobby"));
textGender.setText(Log.d("Screen2Activity", "Gender : + gendertext"));
跟:
Log.d("Screen2Activity", "Name : + editTextText2");
Log.d("Screen2Activity", "Age : + editTextText3");
Log.d("Screen2Activity", "Birthplace : + editTextText4");
Log.d("Screen2Activity", "Hobbies : + selectedhobby");
Log.d("Screen2Activity", "Gender : + gendertext");
请勿尝试使用 的输出。只需在 Logcat 中查看调用在日志记录中发出的内容即可。Log.d()
Log.d()
评论
0赞
Megan
9/18/2023
这解决了提交问题,谢谢!如何获取商店信息以显示在 TextView 中?每当我尝试时,唯一显示的是用于识别字符串的短语。
评论
EditText