2 jsonzhong jsonzhong 于 2016.04.08 11:55 提问

使用RadioGroup设置监听时为啥出现错误,有啥原因造成的 1C

使用RadioGroup设置监听时为啥出现错误,有啥原因造成的

public class MainActivity extends Activity {

private static final long WELCOME_SCHEDULE_TIME = 1000 * 5;  //对时间的延迟5秒
private RadioGroup  radioGroup;
private RadioButton      rb_home,rb_consult,rb_Myindustry,rb_Myordering;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
       initListener();
      initView();
    final Intent intent = new Intent();
    Timer timer = new Timer();

    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            intent.setClass(MainActivity.this, LoginActivity.class);
            MainActivity.this.startActivity(intent);
            finish();
        }
    };

    // 延迟5秒后进入主页面
    timer.schedule(timerTask, WELCOME_SCHEDULE_TIME);
}
/**
 * 控件的获取
 */
private void initView() {
        rb_home= (RadioButton) findViewById(R.id.rb_homepage);
        rb_consult= (RadioButton) findViewById(R.id.rb_consult);
        rb_Myordering= (RadioButton) findViewById(R.id.rb_myordering);
       rb_Myindustry= (RadioButton) findViewById(R.id.rb_myindustry);
}
/**
 * 对所在控件监听
 */
private void initListener() {
      radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(RadioGroup group, int checkedId) {
                       if (checkedId==rb_home.getId()) {
                           Intent intent1 = new Intent(MainActivity.this, HomePage.class);
                           startActivity(intent1);
                       }
                 else if (checkedId==rb_consult.getId()){
                      Intent intent2 = new Intent(MainActivity.this, Consult.class);
                      startActivity(intent2);}
                  else  if (checkedId==rb_Myordering.getId()) {
                        Intent intent3 = new Intent(MainActivity.this, Myordering.class);
                        startActivity(intent3);
                    }
                 else   if (checkedId==rb_Myindustry.getId()) {
                        Intent intent4 = new Intent(MainActivity.this, Mydustry.class);
                        startActivity(intent4);
                    }
              }
      });

}

}

2个回答

A418617870
A418617870   2016.04.08 12:34

是不是忘了 radioGroup = (RadioGroup)this.findViewById(R.id.radioGroup); 这句了

chefanyangzhi
chefanyangzhi   2016.04.08 15:05
  initListener();
  initView();
        这两个换下位置
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!