首页 新闻 赞助 找找看

int A = rg.getCheckedRadioButtonId(); \\这行报空指针异常了。什么个情况?

0
悬赏园豆:50 [已解决问题] 解决于 2015-01-27 11:28

package com.cnhney.eat_mora;


import java.util.Random;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class mora_start extends Activity{
int lpc = 0;
RadioGroup rg = null;
RadioButton rb1 = null;
RadioButton rb2 = null ;
RadioButton rb3 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.mora_start);

Button b55 = (Button) findViewById(R.id.dianji);
RadioGroup rg = (RadioGroup) findViewById(R.id.anniuGroup);
RadioButton rb1 = (RadioButton) findViewById(R.id.shitou1);
RadioButton rb2 = (RadioButton) findViewById(R.id.jiandao1);
RadioButton rb3 = (RadioButton) findViewById(R.id.bu1);


ButtonOnClickListener calculateButtonListener = new ButtonOnClickListener();
b55.setOnClickListener(calculateButtonListener);
}
class ButtonOnClickListener implements OnClickListener {
@Override
public void onClick(View view) {

Random random = new Random();
for(int i = 0; i < 2;i++) {
lpc = (Math.abs(random.nextInt())%10);
}

int A = rg.getCheckedRadioButtonId(); \\这行报空指针异常了。什么个情况?
int B = lpc ;
//B = 0 = 剪刀,B = 1 = 石头 ,B = 2 = 布 。 
if(A != -1 && B != -1) {
if(A == rb1.getId() && B == 0 || 
A == rb2.getId() && B == 2 || 
A == rb3.getId() && B == 1 ) {

Intent intent = new Intent();
intent.setClass(mora_start.this, mora.class);
mora_start.this.startActivity(intent);


}else if(A == rb1.getId() && B == 2 || 
A == rb2.getId() && B == 1 || 
A == rb3.getId() && B == 0 ) {

Intent intent = new Intent();
intent.setClass(mora_start.this, mora1.class);
mora_start.this.startActivity(intent);
}else {
Intent intent = new Intent();
intent.setClass(mora_start.this, mora2.class);
mora_start.this.startActivity(intent);
}
}
}
}


}

===========================
int A = rg.getCheckedRadioButtonId(); \\这行报空指针异常了。什么个情况?

liupengcheng的主页 liupengcheng | 初学一级 | 园豆:124
提问于:2015-01-26 17:30
< >
分享
最佳答案
0

RadioGroup rg = null;

RadioGroup rg = (RadioGroup) findViewById(R.id.anniuGroup); 

这两条语句 声明了两个rg对象,第一个对象为空,

int A = rg.getCheckedRadioButtonId(); 这行的rg引用的是空对象,所以报空指针异常了。

liupengcheng | 初学一级 |园豆:124 | 2015-01-27 11:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册