【问题标题】:Changing onclicklistener for a button from radio buttons从单选按钮更改按钮的 onclicklistener
【发布时间】:2017-01-09 09:28:17
【问题描述】:

您好,我试图让 3 个单选按钮更改一个按钮的 onclick 侦听器,每个单选按钮为该按钮设置自己的点击侦听器。 但我明白了 (MainActivity 不是封闭类)

注意:单选按钮在设置活动中,按钮在主活动中。

设置

public class Settings extends MainActivity {

private RadioGroup radioGroup;
private RadioButton radioButtonpc;
private RadioButton radioButtonps;
private RadioButton radioButtonxb;
public Button settings;
public Button cheatsactivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    cheatsactivity = (Button) findViewById(R.id.cheats_activity);
    settings = (Button) findViewById(R.id.setting_btn);
    radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);
    radioButtonpc = (RadioButton) findViewById(R.id.radioButton1);
    radioButtonxb = (RadioButton) findViewById(R.id.radioButton2);
    radioButtonps = (RadioButton) findViewById(R.id.radioButton3);

    radioButtonpc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cheatsactivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, PC.class);
                    startActivity(intent);
                }
            });
        }
    });

    radioButtonxb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cheatsactivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, XBOX.class);
                    startActivity(intent);
                }
            });
        }
    });
    radioButtonps.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cheatsactivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, PS.class);
                    startActivity(intent);
                }
            });
        }
    });
}




}

更新 它现在给了我 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference whenever i clicked on the radio button 我正在尝试更改以使用户从设置活动中更改主活动中按钮的 onclick 侦听器任何解决方案?

【问题讨论】:

  • 所有这些单选按钮都在R.layout.settings xml 中声明?如果可能,请分享xml代码。
  • 是的,我确实在广播组中声明了它们
  • 将 MainActivity.this 更改为 Settings.this In all Intent

标签: java android radio-button onclicklistener


【解决方案1】:

试试这个代码

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup radioGroup, int i) {
                        if (i == radioButtonpc.getId()) {
                            //your code
                        }
                        if (i == radioButtonxx.getId()) {
                            //your code
                        }
                        if (i == radioButton.getId()) {
                            //your code
                        }
                        if (i == radioButtonab.getId()) {
                            //your code
                        }
                    }
                });

【讨论】:

    【解决方案2】:

    首先, 始终遵循可重用性,因为您使用了 Radio 组,并且通过单击另一个按钮从 Radio 按钮应用了意图。

    使用以下代码 sn-p 后,如果仍然无法正常工作,请在此处更新,

    radioGroup 
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                Log.d("chk", "id" + checkedId);
    
                    if (checkedId == R.id.radioButton1) {
                        //some code
                         Intent intent = new Intent(Settings .this, PC.class);
                        startActivity(intent);
                    } else if (checkedId == R.id.radioButton2) {
                        //some code
                         Intent intent = new Intent(Settings .this, XBOX.class);
                        startActivity(intent);
    
                    }else if (checkedId == R.id.radioButton3) {
                        //some code
                          Intent intent = new Intent(Settings .this, PS.class);
                        startActivity(intent);
                    }
    
                }
    
            }); 
    

    【讨论】:

    • 它给了我 java.lang.IllegalStateException: could not execute method for android:onClick
    • 你能清楚你会在哪里得到 java.lang.IllegalStateException: Could not execute method for android:onClick error 吗?
    • 我修复了这个问题,这是由于我没有声明设置活动造成的,现在我得到 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$ OnClickListener)' 在空对象引用上
    • 你声明了按钮小部件吗?我认为这里不需要按钮单击事件,因为所有操作都通过单选按钮进行管理。
    【解决方案3】:

    您需要使用 living 活动上下文作为new Intent 的第一个参数。

    尝试在您的意图中将MainActivity.this 更改为Settings.this,例如

    Intent intent = new Intent(Settings.this, PC.class);
    

    【讨论】:

      【解决方案4】:

      Settings.this 而不是@howdoidothis 说的MainActivity.this。而且我认为您错误地使用了onClickListener 而不是onCheckChangedListener。请参阅以下示例:

      rd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
      {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
          {
              Intent intent = new Intent(Settings.this, PC.class);
              startActivity(intent);
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-06
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        相关资源
        最近更新 更多