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

时光怂恿深爱的人放手 提交于 2020-06-29 04:14:23

问题


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.


回答1:


You can pass data from one activity to another through Intents. 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.




回答2:


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



来源:https://stackoverflow.com/questions/62183012/how-do-i-pass-an-username-to-another-activity-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!