简体   繁体   中英

First time android programmer can't move between activities

This is my first time programming for android and in java. I am using the latest Android Studio.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_log_in);
    nametxt = (EditText) findViewById(R.id.Txtname);
    final Button nextbutton = (Button) findViewById(R.id.NextButton);
    nametxt.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            nextbutton.setEnabled(!(nametxt.getText().toString().trim().isEmpty()));
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
nextbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

    editor.putString("Name",nametxt.getText().toString());
    editor.apply();
    Intent nextScreen = new Intent(view.getContext(), MainMenu.class);
    startActivity(nextScreen);
}
});

}

In this code I am trying to get the user's name, save it in the android preferences file, and then move to the next screen. Well haha it doesn't work. Any idea why? Thank Alot

Check my comment to the question...

I've specified in the comment to

Try

Intent nextScreen = new Intent(youractivityname.this, MainMenu.class); 

instead of

Intent nextScreen = new Intent(view.getContext(), MainMenu.class); 

Because you will have to pass the current Context as the argument.

try to change last 2 lines into this:

Intent nextScreen = new Intent(this_activity_name.this, MainMenu.class);
startActivity(nextScreen);

where this_activity_name have to be the name of your current activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM