我有三个活动,MainActivity,OtherActivity,ThirdActivity
MainActivity作为主菜单,将通过按钮指向OtherActivity:
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, OtherActivity.class);
MainActivity.this.startActivity(myIntent);
}
然后在OtherActivity中包含需要输入下一个活动的数据表格列表,即ThirdActivity
public class OtherActivity extends Activity {
EditText Name, Age, MPersonal, MBTI, Birthday, SNotes;
Button second button, third button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity);
Intent myIntent = getIntent();
secondbutton = (Button)findViewById(R.id.secondbutton);
thirdbutton = (Button)findViewById(R.id.button3);
Name = (EditText) findViewById(R.id.editText);
Age = (EditText) findViewById(R.id.editText2);
MPersonal = (EditText) findViewById(R.id.editText3);
MBTI = (EditText) findViewById(R.id.editText4);
Birthday = (EditText) findViewById(R.id.editText5);
SNotes = (EditText) findViewById(R.id.editText6);
}
public void onClick2(View view) {
Intent backIntent = new Intent(OtherActivity.this, MainActivity.class);
OtherActivity.this.startActivity(backIntent);
}
public void onClick3(View view) {
Intent bintent = new Intent(this, ThirdActivity.class);
Bundle bbundle = new Bundle();
bbundle.putString("name", Name.getText().toString());
bbundle.putString("age", Age.getText().toString());
bbundle.putString("mper", MPersonal.getText().toString());
bbundle.putString("mbti", MBTI.getText().toString());
bbundle.putString("birthday", Birthday.getText().toString());
bbundle.putString("special", SNotes.getText().toString());
bintent.putExtras(bbundle);
startActivity(bintent);
}
}
它运行良好,直接向ThirdActivity发送意图,即:
public class ThirdActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_activity);
TextView nameView = (TextView) findViewById(R.id.nametext);
nameView.setText(getIntent().getExtras().getString("name"));
TextView ageView = (TextView) findViewById(R.id.agetext);
ageView.setText(getIntent().getExtras().getString("age"));
TextView mperView = (TextView) findViewById(R.id.mpersonalitytext);
mperView.setText(getIntent().getExtras().getString("mper"));
TextView mbtiView = (TextView) findViewById(R.id.mbtitext);
mbtiView.setText(getIntent().getExtras().getString("mbti"));
TextView birthdayView = (TextView) findViewById(R.id.birthdaytext);
birthdayView.setText(getIntent().getExtras().getString("birthday"));
TextView specialView = (TextView) findViewById(R.id.specialtext);
specialView.setText(getIntent().getExtras().getString("special"));
}
Inside ThirdActivity还包含onClick函数并重定向到主菜单:
public void Clicky (View vclicky) {
Intent intentclick = new Intent(ThirdActivity.this, MainActivity.class);
ThirdActivity.this.startActivity(intentclick);
}
效果很好。但是当我尝试返回列出输入数据的ThirdActivity时。我尝试单击按钮时发生错误:
public void onClicky(View vclick) {
Intent kuriki = new Intent(MainActivity.this, ThirdActivity.class);
MainActivity.this.startActivity(kuriki);
}
错误消息说:
E / AndroidRuntime:致命异常:主要 处理:com.example.user.myapplication,PID:3739 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.user.myapplication / com.example.user.myapplication.ThirdActivity}:java.lang.NullPointerException:尝试调用虚方法'java.lang.String android。对象引用上的os.BaseBundle.getString(java.lang.String)' 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在android.app.ActivityThread.-wrap12(ActivityThread.java)
底线是当我尝试在OtherActivity中输入数据时,输入的数据以ThirdView中的TextView形式保存。返回按钮将返回主菜单,即MainActivity。然后当我尝试通过单击指向ThirdActivity的Button来查看数据时。发生错误
你能帮我解释为什么我无法返回输入数据,我认为我不理解错误信息。提前致谢
答案 0 :(得分:1)
很抱歉迟到了,但我已经搜索并尝试了很多解释给你,并知道错误在哪里,你在帖子中阅读/提供的LogCat最初的错误这些行对你没用或者帮助你了解需要的是这个
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.blogspot.dictionary.coding.android.tempdeletemenow.ThirdActivity.onCreate(ThirdActivity.java:32)
描述错误,并在代码中表示行号X(对我来说是32)
nameView.setText(getIntent().getExtras().getString("name"));
首先getIntent().getExtras().getString("name")
表示您从启动此活动的Intent获取保存的数据(Extras),这是
public void onClicky(View vclick) {
Intent kuriki = new Intent(MainActivity.this, ThirdActivity.class);
MainActivity.this.startActivity(kuriki);
}
(在直接启动ViewData的情况下)没有Extras(保存的数据(名称,年龄......)) ,所以来自形式的意图首先输入数据然后视图将在查看 no(null)数据崩溃时起作用
解决方法是: -
<强> 1。 IN ThirdActivit:
getIntent()
意图bintent
bintent
意图,以便在static
喜欢这个
import static com.blogspot.dictionary.coding.android.tempdeletemenow.MainActivity.bintent;
public class ThirdActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_activity);
if (bintent != null) {
if (bintent.hasExtra("name")) {
TextView nameView = (TextView) findViewById(R.id.nametext);
TextView ageView = (TextView) findViewById(R.id.agetext);
TextView mperView = (TextView) findViewById(R.id.mpersonalitytext);
TextView mbtiView = (TextView) findViewById(R.id.mbtitext);
TextView birthdayView = (TextView) findViewById(R.id.birthdaytext);
TextView specialView = (TextView) findViewById(R.id.specialtext);
nameView.setText(bintent.getExtras().getString("name"));
ageView.setText(bintent.getExtras().getString("age"));
mperView.setText(bintent.getExtras().getString("mper"));
mbtiView.setText(bintent.getExtras().getString("mbti"));
birthdayView.setText(bintent.getExtras().getString("birthday"));
specialView.setText(bintent.getExtras().getString("special"));
}
}
}
<强> 2。 IN MainActivity
public class MainActivity extends Activity {
static Intent bintent;
@Override
protected void onCreate(Bundle savedInstanceState) {
第3。最后,IN OtherActivity
import static com.blogspot.dictionary.coding.android.tempdeletemenow.MainActivity.bintent;
并在Intent
bintent = new Intent(this, ThirdActivity.class);