gpt4 book ai didi

java - 另一个 Activity 中的单选按钮仍然为空?

转载 作者:行者123 更新时间:2023-12-01 19:08:07 26 4
gpt4 key购买 nike

在一项 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 方法下。我的按钮仍然为空。

我做错了什么?

最佳答案

Activity 的布局是否相同?如果它们不相同,请检查 R.id.miles_btn 的导入

这里还有另一个问题:

        if(miles_btn.equals(milesbutton)) 

miles_btn 是 RadioButton,milesbutton 是 boolean 值,因此您必须通过以下方式更改代码:

    if(miles_btn.isChecked() == milesbutton) 

关于java - 另一个 Activity 中的单选按钮仍然为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59512359/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com