简体   繁体   中英

How do I pass an Username to another Activity in Android Studio

I'm currently writing my first App and I want to greet a User by their Name. On the Main acitity I created a Textview called textview_username which i want to "fill" with the users login name. I tried to work with sharedPrefrences but the App keeps crashing when I open the MainActivity.

That is the registration page.

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{ EditText username, password; Button show, register, delete;

public static final String myprefnm= "Nickname";
public static final String myprefpw = "pass";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    username = findViewById(R.id.txte_username);
    password = findViewById(R.id.txte_password);
    show = findViewById(R.id.btn_show);
    register = findViewById(R.id.btn_register);
    delete = findViewById(R.id.btn_delete);
    show.setOnClickListener((View.OnClickListener)this);
    register.setOnClickListener((View.OnClickListener)this);
    delete.setOnClickListener((View.OnClickListener)this);
}

@Override
public void onClick(View v) {

    if(v == show){


        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(myprefnm, 0);
        SharedPreferences sharedPreferencespw = getApplicationContext().getSharedPreferences(myprefpw, 0);


        String usrname = sharedPreferences.getString(myprefnm, "1");


        username.setText(usrname);
        String pass = sharedPreferencespw.getString(myprefpw, "2");
        password.setText(pass);
        Toast.makeText(this, "Daten werden angezeigt",Toast.LENGTH_SHORT).show();
    }
    if(v == register){

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(myprefnm, 0);
        SharedPreferences sharedPreferencespw = getApplicationContext().getSharedPreferences(myprefpw, 0);


        SharedPreferences.Editor editor = sharedPreferences.edit();
        SharedPreferences.Editor editorpw = sharedPreferencespw.edit();


        editor.putString(myprefnm, username.getText().toString());
        editorpw.putString(myprefpw, password.getText().toString());


        editor.commit();
        editorpw.commit();
        Toast.makeText(this, "Registrierung erfolgreich", Toast.LENGTH_SHORT).show();
        Intent openSetPin = new Intent(RegisterActivity.this, SetPinActivity.class);
        startActivity(openSetPin);
    }


    if (v==delete){
        username.setText("");
        password.setText("");
        Toast.makeText(this, "Eingaben gelöscht", Toast.LENGTH_SHORT).show();
    }
}





public class MainActivity extends AppCompatActivity {
public static final String myprefnm= "Nickname";
// Variables
TextView username;
SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences(myprefnm, 0);
float x1, x2, y1, y2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String username = sharedPrefs.getString(myprefnm, "1");
    this.textview_username.setText(username);
    }
}

I used Try and Catch so I think the Error is caused by the sharedpreferences.

You can pass data from one activity to another through Intent s. All you need is the data itself and a key which points to the first.

Intent openSetPin = new Intent(RegisterActivity.this, SetPinActivity.class);
String name =  username.getText().toString();
// Add the data to the intent using the a key
openSetPin.putExtra("name_key", name);
startActivity(openSetPin);

On the other side ( SetPinActivity ) you can extract the data using the same key as follows,

// Getting the intent which started this activity
Intent intent = getIntent();
// Get the data of the activity providing the same key value
String name = intent.getStringExtra("name_key");

You may achieve the same using SharedPreferences but this persists the data (saves a file on the disk) and you don't need that here.

Hope you added All activity in the AndroidManifest.xml file. Use the Intent to pass the data between the activities instead of sharedprefs. As suggested by @Themelis

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