EDIT This is the full code from the first activity:
public class login extends AppCompatActivity {
dbhelper mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mydb = new dbhelper(this);
Button btn = (Button) findViewById(R.id.Buttonregis);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent i = new Intent(login.this,Reg.class);
startActivity(i);
}
});
Button lgn = (Button) findViewById(R.id.btlogin);
lgn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//AMBIL CONTENT EDIT_TEXT
EditText txtusername = (EditText)findViewById(R.id.ETusername);
EditText txtpassword = (EditText)findViewById(R.id.ETpassword);
//CONVERT EDIT_TEXT MENJADI STRING
String user = txtusername.getText().toString();
String pass = txtpassword.getText().toString();
//PANGGIL FUNGSI VALIDasi DARI kelas dbhelper
if(mydb.checkvalid(user,pass) == true){
Intent i = new Intent(login.this,Menu.class);
i.putExtra("nama",user);
startActivity(i);
}else{
Toast.makeText(getApplicationContext(), "Wrong ID/password", Toast.LENGTH_SHORT).show();
}
}
});
EDIT This is how I retreive it from the 2nd activity:
public class Menu extends AppCompatActivity {
dbhelper mydb;
Intent intent = getIntent();
String user = intent.getExtras().getString("nama");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button babout = (Button) findViewById(R.id.btabout);
babout.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent about = new Intent(Menu.this,About.class);
startActivity(about);
}
});
Button bprofile = (Button) findViewById(R.id.btprofile);
bprofile.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent profile = new Intent(Menu.this,Profile.class);
//profile.putExtra("bknnama",user);
startActivity(profile);
}
});
//select
Button bcheck = (Button) findViewById(R.id.btcheck);
bcheck.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent check = new Intent(Menu.this,Reg.class);
startActivity(check);
}
});
Button bnovel = (Button) findViewById(R.id.btnovel);
bnovel.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent about = new Intent(Menu.this,Novel_list.class);
startActivity(about);
}
});
}
} The system declared that there was no error, but the moment I opened the app and press the corresponding button to get to another activity, My app forced close.
When I made the Intent code into comment, the app works fine again, but I need the user string to be pass to another activity.
EDIT This is the logcat reply
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference