我正在单击按钮时更新一个 Activity 中 boolean 变量的值,并在启动屏幕 Activity 中使用结果,但它给出了空指针异常。我做错了什么以及如何解决它?
我创建了 2 个按钮,其值随按下哪个按钮而改变。并在另一个 Activity 中使用 boolean 值。但它给出了错误
//The Activity where boolein updates on button click
public class ProfileSelection extends AppCompatActivity {
private Toolbar mToolbar;
private Button signs, text;
boolean impaired = false; //Boolean that i neededto update
//FIrebase
FirebaseUser currentUser;
FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_selection);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle(" Hand Talk Mediator");
getSupportActionBar().setIcon(R.drawable.logosmall);
text = (Button)findViewById(R.id.with_text);
signs = (Button)findViewById(R.id.with_signs);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
impaired =false;
Intent intent = new Intent(ProfileSelection.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
});
signs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
impaired = true;
Intent intent = new Intent(ProfileSelection.this,MainActivitySign.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
});
}
//The activity where boolean is needed in condition
public class SplashScreen extends AppCompatActivity {
private static int SPLASH_TIMEOUT = 1200;
Context context;
private boolean selection = ((ProfileSelection)context).impaired;
//private boolean selection = true;
//FIrebase
FirebaseUser currentUser;
FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
}
@Override
protected void onStart() {
super.onStart();
new Handler().postDelayed(new Runnable() {
@Override
public void run()
{
if (currentUser == null){
sendUsertoLoginActivity();
}
else
{
if (selection== false)
{
Intent homeIntent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(homeIntent);
finish();
}
else if (selection == true)
{
Intent homeIntentSign = new Intent(SplashScreen.this,MainActivitySign.class);
startActivity(homeIntentSign);
finish();
}
else {
Intent loginIntent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(loginIntent);
finish();
}
},SPLASH_TIMEOUT);
}
最佳答案
您在 SplashScreen 中定义 boolean 值的方式可能不是您想要的。 定义如下
boolean selection;
还可以在 ProfileSelection Activity 中按照您的 Intent 添加额外内容
Intent intent = new Intent(ProfileSelection.this,MainActivity.class);
intent.putExtra("Impaired", impaired);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
这里“Impaired”用作键,变量损坏用作值,就像在 HashMap 中一样
现在转到 SplashScreen Activity 并在 onCreate() 方法中初始化选择
Intent intent = getIntent();
selection = intent.getExtra("Impaired");
现在选择变量包含第一个 Activity 中受损的值,您可以在第二个 Activity 中使用它。
关于java - 如何在另一个 Activity 的 if-else 语句中使用来自单独 Activity 的 boolean 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56549015/