I want to make an app that gets user input from the first activity and sets these values to a class and shows them in the other activity.
The error I have is null pointer exception when i try to get the values in the second class. I don't understand what I do wrong.
This is the first activity:
public class MainActivity extends Activity {
EditText name, phone, type;
Button addBtn;
public final static String SER_KEY = "com.easyinfogeek.objectPass.ser";
public final static String Tag = "Yes";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addBtn = findViewById(R.id.button);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SerializeMethod();
}
});
}
public void SerializeMethod(){
name = findViewById(R.id.name);
phone = findViewById(R.id.phone);
type = findViewById(R.id.type);
PhoneBookEntry book = new PhoneBookEntry();
book.setName(name.toString());
book.setPhone(phone.toString());
book.setType(type.toString());
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable(SER_KEY, book);
startActivity(intent);
}
}
Second activity:
public class SecondActivity extends Activity {
final public static String Tag = "Yes";
TextView txt1, txt2, txt3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
txt1 = findViewById(R.id.textView1);
txt2 = findViewById(R.id.textView2);
txt3 = findViewById(R.id.textView3);
PhoneBookEntry book = (PhoneBookEntry) getIntent().getSerializableExtra(MainActivity.SER_KEY);
txt1.setText(book.getName());
txt2.setText(book.getPhone());
txt3.setText(book.getType());
}
}
and this is the PhoneBookEntry class:
public class PhoneBookEntry implements Serializable {
private static final long serialVersionUID = -7060210544600464481L;
private String name;
private String phone;
private String type;
--- getters and setters ---
}