【问题标题】:Having an issue with Radiogroup in Android在 Android 中遇到 Radiogroup 的问题
【发布时间】:2015-02-25 11:07:44
【问题描述】:

我在 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>

我的代码有很多东西,我面临的问题只有 RadioGroup,我在启动活动时出错,

问题出在以下行:rg.setOnCheckedChangeListener(new OnCheckedChangeListener()

我在另一个 ActionBarActivity 上测试了相同的代码(仅 RadioGroup 的),它工作正常,那么这个 Activity 类有什么问题?

【问题讨论】:

    标签: android radio-group


    【解决方案1】:

    问题出在以下行:rg.setOnCheckedChangeListener(new OnCheckedChangeListener()

    rgnull,因为在调用setContentView 之前尝试从布局中获取视图。所以交换两行:

    setContentView(R.layout.activity_questions);
    RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      • 2017-05-21
      • 2012-06-10
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      相关资源
      最近更新 更多