来自另一个 activity 的单选按钮仍然为空?

Radio Button from another activity remains null?

在一个 activity 中,我有我的单选按钮。我能够在整个应用程序中保存单选按钮的选定状态。在另一个 activity 中,我想检查选中了哪个单选按钮,但单选按钮一直返回 null。

这是第一个activity:

sharedPreferences = getApplicationContext().getSharedPreferences(GetInfo, MODE_PRIVATE);


go_back_btn = (Button) findViewById(R.id.go_back_btn);
distancebtn_group = findViewById(R.id.distance_btn_group);
miles_btn = (RadioButton) findViewById(R.id.miles_btn);
kilometer_btn = (RadioButton) findViewById(R.id.kilometer_btn);


go_back_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        sharedPreferences = getApplicationContext().getSharedPreferences(GetInfo, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(MILESBTN, miles_btn.isChecked());
        editor.putBoolean(KMBTN, kilometer_btn.isChecked());
        editor.apply();
        Intent intent = new Intent(settings.this, home.class);
        intent.putExtra(MILESBTN, miles_btn.isChecked());
        intent.putExtra(KMBTN, kilometer_btn.isChecked());
        startActivity(intent);
    }
});

kilometer_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        sharedPreferences = getApplicationContext().getSharedPreferences(GetInfo, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(KMBTN, kilometer_btn.isChecked());
        editor.apply();


    }
});
miles_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        sharedPreferences = getApplicationContext().getSharedPreferences(GetInfo, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(MILESBTN, miles_btn.isChecked());
        editor.apply();
    }
});


sharedPreferences = getApplicationContext().getSharedPreferences(GetInfo, MODE_PRIVATE);
kilometer_btn.setChecked(sharedPreferences.getBoolean("kilometer_btn", true));
miles_btn.setChecked(sharedPreferences.getBoolean("miles_btn", false));

}

这是我的第二个 activity:

    distance_counter = (TextView) findViewById(R.id.distance_counter);
    distancebtn_group = (RadioGroup) findViewById(R.id.distance_btn_group);
    miles_btn = (RadioButton) findViewById(R.id.miles_btn);
    kilometer_btn = (RadioButton) findViewById(R.id.kilometer_btn);
    distancebtn_group = findViewById(R.id.distance_btn_group);




    final SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(settings.GetInfo, MODE_PRIVATE);
    final Boolean milesbutton = sharedPreferences.getBoolean(MILESBTN, false);
    final Boolean kilometerbutton = sharedPreferences.getBoolean(KMBTN, true);

    Boolean milesbtn = getIntent().getBooleanExtra(MILESBTN, false);
    Boolean kilometerbtn = getIntent().getBooleanExtra(KMBTN, true);
    getIntent().getBooleanExtra(MILESBTN, false);
    getIntent().getBooleanExtra(KMBTN, true);



            }
        }
    });
}


@Override
public void onLocationChanged(Location location) {
    curr_location = location;
    if (start_location == null) {
        start_location = curr_location;
        end_location = curr_location;
    } else
        end_location = curr_location;


    ChooseMetricUnits();
    distance_counter.setText(new DecimalFormat("#.##").format(distance));

    current_speed = location.getSpeed() * 2.236936;
    SpdInmph.setText(new DecimalFormat("0.00").format(current_speed));

    current_speed = location.getSpeed() * 3.6;
    SpdInkmh.setText(new DecimalFormat("0.00").format(current_speed));

}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}


@Override
public void onProviderDisabled(String provider) {

}


private void distanceInMeters() {
    distance = distance + (start_location.distanceTo(end_location));
    start_location = end_location;

}

private void distanceInMiles() {
    distance = distance + (start_location.distanceTo(end_location) * 0.00062137);


}

private void distanceInkilometers() {
    distance = distance + (start_location.distanceTo(end_location) / 1000);
    start_location = end_location;


}




public void ChooseMetricUnits() {
    final SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(settings.GetInfo, MODE_PRIVATE);
    Boolean milesbutton = sharedPreferences.getBoolean(MILESBTN, false);
    Boolean kilometerbutton = sharedPreferences.getBoolean(KMBTN, true);
    Boolean milesbtn = getIntent().getBooleanExtra(MILESBTN, true);
    Boolean kilometerbtn = getIntent().getBooleanExtra(KMBTN, true);
    getIntent().getBooleanExtra(MILESBTN, false);
    getIntent().getBooleanExtra(KMBTN, true);
    if(miles_btn.equals(milesbutton)) {
        distanceInMiles();

: 运行

 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
    at mobile.run_interface.ChooseMetricUnits(run_interface.java:325)

enter image description here

如您所见,问题出在这里:

        if(miles_btn.equals(milesbutton)) {

我同时使用了 putBoolean 和 putExtra 来查看当我从其中一个中提取值时是否会发生不同的事情。同样的结果。我已将用于检索值的代码放置在 create 下,也放置在我的 ChooseMetricsUnits void 方法下。我的按钮仍然是空的。

我做错了什么?

活动布局一样吗?如果它们不相同,请检查 R.id.miles_btn

的导入

这里还有一个问题:

        if(miles_btn.equals(milesbutton)) 

miles_btn 是 RadioButton 而 milesbutton 是一个布尔值,所以你必须通过以下方式更改代码:

    if(miles_btn.isChecked() == milesbutton) 

解决了问题:

希望这对以后的其他人有帮助...\

    private void ChooseMetricUnits() {
    final SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(GetInfo, MODE_PRIVATE);
    final Boolean MetricUnit = sharedPreferences.getBoolean(settings.KMBTN, true);
    if(MetricUnit) {
        distanceInkilometers();
    } else
        distanceInMiles();
    }

}