我在Android中遇到了RadioGroup的问题,我测试了一切,但它似乎没有用。
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
setContentView(R.layout.activity_questions);
titre = (TextView) findViewById(R.id.textView1);
question = (TextView) findViewById(R.id.textView2);
image = (ImageView) findViewById(R.id.imageView1);
rep2 = (RadioButton) findViewById(R.id.radio1);
rep3 = (RadioButton) findViewById(R.id.radio2);
rep4 = (RadioButton) findViewById(R.id.radio3);
count = (TextView) findViewById(R.id.textView4);
counter = (TextView) findViewById(R.id.textView3);
count.setText("1/5");
try {
result = new RequestTask().execute("http://tv3.orangeadd.com/surveys/current?owner=ofc&user=yasser&category=-1").get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
s = new Survey();
s = gson.fromJson(result, Surveyentity.class).Survey;
titre.setText(s.getName());
question.setText(s.getListquest().get(0).getLabel());
Picasso.with(Questions.this).load(s.getListquest().get(0).getUrl()).into(image);
rep1.setText(s.getListquest().get(0).getList().get(0).getLabel());
rep2.setText(s.getListquest().get(0).getList().get(1).getLabel());
rep3.setText(s.getListquest().get(0).getList().get(2).getLabel());
rep4.setText(s.getListquest().get(0).getList().get(3).getLabel());
start = new CountDownTimer(s.getListquest().get(0).getTimeout(), 1000) {
public void onTick(long millisUntilFinished) {
counter.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
counter.setText("done!");
prochainequestion();
}
}.start();
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.radio0) {
Toast.makeText(getApplicationContext(), "choice: Silent",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.radio1) {
Toast.makeText(getApplicationContext(), "choice: Sound",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "choice: Vibration",
Toast.LENGTH_SHORT).show();
}
}
});
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.orange.v1.Questions" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="22sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/imageView1"
android:text="1/5" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignLeft="@+id/textView1"
android:layout_alignTop="@+id/textView3"
android:src="@drawable/ic_action_time" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_toRightOf="@+id/imageView2"
android:text="60s" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/imageView1"
android:text="Question ??????????"
android:textSize="20sp" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginTop="18dp"
android:layout_toRightOf="@+id/imageView2" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
</RelativeLayout>
&#13;
我的代码有很多东西,我面临的问题只有RadioGroup,我在启动活动时出错,
问题在于:rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
我在另一个ActionBarActivity上测试了相同的代码(只有RadioGroup),它运行正常,那么这个Activity类有什么问题?
答案 0 :(得分:0)
问题在于:rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
rg
为null
,因为在调用setContentView
之前尝试从布局中获取View。所以交换两行:
setContentView(R.layout.activity_questions);
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);