I what to send a bundle with an ArrayList
, but my code causes a NullPointerException
. But I don't know why.
Main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act1);
context = getApplicationContext();
txtName = findViewById(R.id.name);
txtDepartment = findViewById(R.id.department);
txtUndergred = findViewById(R.id.undergred);
txtUrl = findViewById(R.id.url);
txtPhone = findViewById(R.id.phone);
btnphone = findViewById(R.id.btnphone);
btnlogin = findViewById(R.id.btnlogin);
btnweb = findViewById(R.id.btnweb);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = txtName.getText().toString();
String department = txtDepartment.getText().toString();
String undergrad = txtUndergred.getText().toString();
Bundle Sender = new Bundle();
ArrayList list = new ArrayList<>();
list.add(name);
list.add(department);
list.add(undergred);
Sender.putStringArrayList("list", list);
ArrayList shit = Sender.getStringArrayList("list");
Intent intent = new Intent(getApplicationContext(),MainActivity2.class);
intent.putExtra("Sender", Sender);
startActivity(intent);
}
});
}
MainActivity2(receive here)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
txtUrl = findViewById(R.id.url);
txtPhoneNum = findViewById(R.id.phoneNum);
button0 = findViewById(R.id.goback);
Intent passedIntent = getIntent();
if (passedIntent != null) {
Bundle mybundle = passedIntent.getBundleExtra("Sender");
ArrayList list = mybundle.getStringArrayList("Sender");
Toast.makeText(getApplicationContext(), "Student info: "
+list.get(0)+list.get(1)+list.get(2),duration).show();
}
String Url = txtUrl.getText().toString();
String PhoneNum = txtPhoneNum.getText().toString();
}
Here, which part is wrong? Arraylist
is added to the bundle but every time a NullPointerException
is thrown.