[英]How to use boolean variable from separate activity in if- else statement of another activity
我在按钮单击时更新一个活动中的布尔变量的值,并在启动画面活动中使用结果但它给出了空指针异常。 我做错了什么,我该如何解决?
我创建了2个按钮,它的值变化了按下哪个按钮。 并在另一个活动中使用布尔值。 但它给出了错误
//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);
}
而不是直接访问受损值:
private boolean selection = ((ProfileSelection)context).impaired;
你应该通过Bundle传递它。 然后通过getIntent().getBundleExtra().getBoolean()
在活动中提取它getIntent().getBundleExtra().getBoolean()
你如何在SplashScreen中定义布尔值可能不是你想要的。 定义它就像
boolean selection;
在ProfileSelection活动中也像这样把putExtra与你的意图放在一起
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);
在这里,“受损”作为关键和可变受损工作,就像HashMaps中的价值一样
现在转到SplashScreen活动并在onCreate()方法中初始化选择
Intent intent = getIntent();
selection = intent.getExtra("Impaired");
现在,选择变量包含第一个活动受损的值,您可以在整个第二个活动中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.