我目前正在学习明确的意图。并且我决定要有点创造力,并为三种活动创建明确的意图。 MainActivity,activity2和activity3。在activity1中,我希望能够填写名字和姓氏,然后在我单击提交时,全名将显示在activity2的第一个文本视图中。然后在activity2中有一个按钮,上面写着“单击此处输入出生日期”,然后将其带到activity3中,我应该在其中输入出生日期并点击Submit。点击提交时,出生日期将在全名textview之后的第二个textview中显示。
相反,它会继续停止并返回nullpointer异常
这是MainActivity中编写的代码:
EditText etFirst, etSecond;
Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etFirst=findViewById(R.id.etFirst);
etSecond=findViewById(R.id.etSecond);
btnSubmit=findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(etFirst.getText().toString().isEmpty())
{
Toast.makeText(MainActivity.this, "Enter First Name!", Toast.LENGTH_SHORT).show();
}
else if (etSecond.getText().toString().isEmpty())
{
Toast.makeText(MainActivity.this, "Enter Last Name!", Toast.LENGTH_SHORT).show();
}
else
{
Intent intent= new Intent(MainActivity.this, com.example.nameintent.Activity2.class);
String first= etFirst.getText().toString().trim();
String second=etSecond.getText().toString().trim();
intent.putExtra("first", first);
intent.putExtra("second",second);
startActivity(intent);
MainActivity.this.finish();
}
}
});
}
这是活动2中的代码:
TextView tvName, tvBirth;
Button btnAct3;
final int ACTIVITY3=3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
tvName=findViewById(R.id.tvName);
tvBirth=findViewById(R.id.tvBirth);
btnAct3=findViewById(R.id.btnAct3);
tvName.setVisibility(View.GONE);
String first= getIntent().getStringExtra("first");
String second= getIntent().getStringExtra("second");
String full= first +" "+second;
tvName.setText(full);
tvName.setVisibility(View.VISIBLE);
btnAct3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(Activity2.this, com.example.nameintent.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)
{
tvBirth.setText(data.getStringExtra("dob"));
}
}
if (resultCode==RESULT_CANCELED)
{
String nDob="No Data Received!";
tvBirth.setText(nDob);
tvBirth.setVisibility(View.VISIBLE);
}
}
}
这是我在Activity3中编写的代码:
EditText etDob;
Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3);
etDob=findViewById(R.id.etDob);
btnSubmit=findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (etDob.getText().toString().isEmpty())
{
Toast.makeText(Activity3.this, "Please Enter Date Of Birth!", Toast.LENGTH_SHORT).show();
}
else
{
Intent intent=new Intent(Activity3.this, com.example.nameintent.Activity2.class);
String dob= etDob.getText().toString().trim();
intent.putExtra("dob",dob);
setResult(RESULT_OK);
Activity3.this.finish();
}
}
});
}
空点异常发生在
tvBirth.setText(data.getStringExtra("dob")); in activity2