0

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

3 Answers3

0

The code is wrong. You must get the intent in the OnCreate method:

public class Menu extends AppCompatActivity {
dbhelper mydb;
//....
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_menu);
      Intent intent = getIntent();
      String user = intent.getExtras().getString("nama");
      // ..rest of your code...
    }
}

Hope it helps. Regards, Cs

Csongi77
  • 329
  • 3
  • 13
-1

Change your code from this

 Intent intent = getIntent();
 String user = intent.getExtras().getString("nama");

to

 Intent intent = getIntent();
 String user = intent.getStringExtra("nama");
Ramesh Yankati
  • 1,197
  • 9
  • 13
  • While this is a good suggestion as it simplifies the function call, it will still throw an NPE because `intent` is `null`. – Code-Apprentice Dec 06 '18 at 16:46
  • how does getIntent returns null ?.Ideally getIntent() returns the intent that started this activity – Ramesh Yankati Dec 06 '18 at 16:49
  • I don't know. But that is the conclusion I get from the given error message. I believe we need a more complete code example from the OP before we can give a correct answer. – Code-Apprentice Dec 06 '18 at 16:50
  • Perhaps more accurately, the `intent` variable in the line `String user = intent.getExtras().getString("nama");` is `null`. I assume this is the line which throws the exception since it is the only one with a call to `getExtras()`. But I also suspect that the error occurs outside the code given by the OP. – Code-Apprentice Dec 06 '18 at 16:54
  • As adb logs says Intent is null for some reason... I thought extras is null ,that is nothing but its Bundle object would be returned by intent.getExtras() – Ramesh Yankati Dec 06 '18 at 16:57
  • If `intent` is `null`, `intent.getExtras()` will throw a NPE, not return a Bundle. – Code-Apprentice Dec 06 '18 at 16:59
-3

Make sure you declared the your Menu.class inside AndroidManifest.xml

Kane Hamath
  • 15
  • 1
  • 4