我目前正在编写我的第一个应用程序,我想用用户的名字来问候他们。在主活动中,我创建了一个名为textview_username的Textview,我想用用户的登录名来 "填充"。我试着用sharedPrefrences工作,但当我打开MainActivity时,App一直崩溃。
这就是注册页面。
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);
}
}
我使用了Try和Catch,所以我认为错误是由sharedpreferences引起的。
你可以从一个活动传递数据到另一个活动通过 Intent
s.你所需要的只是数据本身和。钥匙 其中指向第一个。
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);
在另一边(SetPinActivity
)你可以使用 同键 以下是:
// 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");
您可以使用 SharedPreferences
但这会持久化数据(在磁盘上保存一个文件),而你在这里不需要。
希望你在AndroidManifest.xml文件中添加了All activity.使用Intent在活动之间传递数据,而不是sharedprefs。按照@Themelis的建议