如何在其他活动的if- else语句中使用来自单独活动的布尔变量

时间:2019-06-11 17:35:00

标签: java android

我要在一次单击按钮的活动中更新布尔变量的值,并在启动画面活动中使用结果,但它给出了空指针异常。我在做什么错,我该如何解决?

我已经创建了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);

    }

2 个答案:

答案 0 :(得分:0)

而不是直接获得减值:

private boolean selection = ((ProfileSelection)context).impaired; 

您应该通过Bundle通过意图传递它。然后通过getIntent().getBundleExtra().getBoolean()

将其提取到活动中

答案 1 :(得分:0)

如何在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");

现在选择变量包含第一个活动中的受损值,您可以在第二个活动中使用它。