如何检查在另一个活动中选中了哪个单选按钮,然后在IF语句中使用它?

时间:2019-12-11 14:02:02

标签: java android

我正在开发健身应用。在“设置”活动中,我创建了选择英里或公里的选项,如下所示。 Image Radio Buttons

然后是我的run_interface活动。 Distance textview

如果有人单击“英里”单选按钮(在“设置”活动中),则距离应输出run_interface Activty中指定的distanceInmiles方法;如果他们单击“千米”单选按钮,则距离应输出distanceInkilometers方法

这是我的设置活动

public class settings extends AppCompatActivity {

    RadioGroup distancebtn_group;
    RadioButton miles_btn;
    RadioButton kilometer_btn;
    RadioButton distance_button_clicked;
    RadioButton radioButton;


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

        distancebtn_group = findViewById(R.id.distance_btn_group);

    public void buttonCheck(View view) {
        boolean buttonChecked = ((RadioButton) view).isChecked();

        switch (view.getId()) {
            case R.id.miles_btn:
                if (buttonChecked)
                    break;

            case R.id.kilometer_btn:
                if (buttonChecked)
                    break;

这是我的run_interface:

 @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;

        //(Update UI here(could be distanceInMiles or distanceInkilometers, depending on button selected)   
        distance_counter.setText(new DecimalFormat("0.00").format(distance));


        }
    }


    @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;

我对当前代码进行了一些修改。我可以保持所选单选按钮之一的选定状态。

我唯一的问题是单选按钮需要在其他活动中检查其状态时返回null

我在想,因为我的单选按钮驻留在settings活动中,所以run_interface活动无法读取它们,但是我已经发送并检索了所需的 通过sharedpreference和intents的值。

我什至决定同时使用putExtra和putBoolean来查看它们之一是否可以工作。仍然失败。

这是我当前的设置活动

    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));

}

}

现在这是我的run_interface活动的一部分

        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);


    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;

//(Update UI here(could be distanceInMiles or distanceInkilometers, depending on button selected)
    distance_counter.setText(new DecimalFormat("#.##").format(distance));


}


@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);
    final Boolean milesbutton = sharedPreferences.getBoolean(MILESBTN, false);
    final 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();
    }
}

}

:Run
  Process: Mobile.apps, PID: 12173
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:331)

enter image description here

您可以看到问题在这里...

        if(miles_btn.equals(milesbutton)) {

3 个答案:

答案 0 :(得分:0)

无论您在设置中提供了什么选项,都应该将其保存在某个地方,对吗? 从保存的数据中获取值,然后验证是否进行了检查。

答案 1 :(得分:0)

您可以为此目的使用SharedPreferences

首先在radioButton clicklistener上进行定义。

radiobutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
 if(radioButton.isChecked()){

SharedPreferences pref = mContext.getSharedPreferences("MyPref", 0); // 0 - for private mode
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("radiobutton", "checked");
        editor.commit();

 }
        }
    });

第二次活动

    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
    String radiobutton = pref.getString("radiobutton", null);

 if(radiobutton.equals("checked){
 //run your code
 }

这会很好.....

答案 2 :(得分:0)

最后找到了解决方法

app.use(express.static('dist/<project-name>'));

我只是从第一个活动(设置)中提取了一个布尔值(KMBTN),并在选中相应按钮时使用它来调用正确的方法。

要检查其是否正常运行,我在公里无效方法中添加了“ km”,在英里无效方法中添加了“ miles”。

我希望这对以后遇到同样问题的人有帮助。

java android