简体   繁体   中英

startActivity sending to wrong activity: Android

I'm trying to have it to where after the my "Overview" Button is clicked the next button "Variables" is enabled. The button enables, however the setOnClickListener for my "Variables" sends me to my OverviewActivity, rather than my VariablesActivity. I've been stuck on this for a while.

The Variables Activity is just a multiple choice screen, but i was just setting it up.

I'm really new to Java and Android Studio, so this is getting confusing fast

Code:

public class HomeActivity extends AppCompatActivity {

public Button signOutBtn;
public Button overviewBtn;
public Button varBtn;
FirebaseAuth auth;
boolean overviewDone;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    signOutBtn = findViewById(R.id.signOutBtn);
    overviewBtn = findViewById(R.id.overviewBtn);
    varBtn = findViewById(R.id.varBtn);
    auth = FirebaseAuth.getInstance();

    overviewDone = false;

    signOutBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            auth.signOut();
            startActivity(new Intent(HomeActivity.this, SignInActivity.class));
            finish();
        }
    });

    overviewBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent overviewBtnIntent = new Intent(HomeActivity.this, OverviewActivity.class);
            startActivity(overviewBtnIntent);
        }
    });

    if (getIntent().hasExtra("state")) {
        if (getIntent().getStringExtra("state").equals("enable")) {
            overviewDone = true;
            varBtn.setEnabled(true);
        } else {
            varBtn.setEnabled(false);
        }
    } else {
        varBtn.setEnabled(false);
    }

    varBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent varbiesButtonIntent = new Intent(HomeActivity.this, VariablesActivity.class);
            startActivity(varbiesButtonIntent);
            Log.i("Variable Button", "Going to Variable Activity");
        }
    });
}

}

public class OverviewActivity extends AppCompatActivity {

public Button backBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_overview);

    backBtn = findViewById(R.id.ov_backButton);

    backBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(OverviewActivity.this, HomeActivity.class);
            intent.putExtra("state", "enable");
            startActivity(intent);
        }
    });
}

}

public class VariablesActivity extends AppCompatActivity {

public RadioGroup radioGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_variables);

    radioGroup = (RadioGroup)findViewById(R.id.RadioGroup);

    // getting value of radio button checked
    final String value = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId())).getText().toString();


    if(radioGroup.getCheckedRadioButtonId() == -1){
        // no radio buttons checked
    } else{
        // one the radio buttons are checked
        Log.i("Radio Button Clicked", value);
    }
}

}

Stacktrace:

2021-11-22 21:45:23.550 4535-4535/com.example.codehorizonapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.codehorizonapplication, PID: 4535
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.codehorizonapplication/com.example.codehorizonapplication.VariablesActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference
    at com.example.codehorizonapplication.VariablesActivity.onCreate(VariablesActivity.java:24)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
int checkedId = radioGroup.getCheckedRadioButtonId();
String value = "";
if(checkedId != 1){
   value = ((RadioButton)findViewById(checkedId)).getText().toString();
}

radioGroup.getCheckedRadioButtonId() returns -1 if there is nothing checked, -1 isn't a valid Id thus findViewById returns null.

null.getText() is a java.lang.NullPointerException

This is under the assumption that there is nothing selected and the fact that calling radioGroup.getCheckedRadioButtonId() doesn't crash only when you call getText() does it crash means your radioGroup = (RadioGroup)findViewById(R.id.RadioGroup); is correct.

The reason the OverviewActivity is created instead is due to a stack problem caused by the premature end to VariablesActivity due to the exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM