Android中的Radiogroup遇到问题

陌生人B.

我在使用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类怎么了?

ρяσsρєяK

问题出在那一行:rg.setOnCheckedChangeListener(new OnCheckedChangeListener()

rg这是null因为尝试在调用之前从布局获取View setContentView所以交换两行:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Android中的SQLite中创建表时遇到问题

来自分类Dev

在Android中更改ActionMode背景颜色时遇到问题

来自分类Dev

我在Android Studio中运行抖动时遇到问题

来自分类Dev

在Android Studio中添加图像源时遇到问题

来自分类Dev

路由问题,在 Laravel 中遇到问题

来自分类Dev

安装软件遇到问题-Android

来自分类Dev

安装软件遇到问题-Android

来自分类Dev

运行 Android Studio 时遇到问题

来自分类Dev

在LocalPlayerActivity chromecast中遇到问题

来自分类Dev

MERGE SQL查询中遇到问题

来自分类Dev

.htaccess重写URL中遇到问题

来自分类Dev

在 Ruby 的 Knights Tour 中遇到问题

来自分类Dev

我遇到问题的 sql 中的分组

来自分类Dev

在Golang中遇到gzip.Reader遇到问题

来自分类Dev

在 HashMap<String,ArrayList<Model>> 中遇到问题(在 Android 中创建评论视图)

来自分类Dev

IgnoreCase遇到问题

来自分类Dev

.bind遇到问题

来自分类Dev

JOPtionPane遇到问题

来自分类Dev

HEREDOC遇到问题

来自分类Dev

jmHighlight遇到问题

来自分类Dev

chmod遇到问题

来自分类Dev

在tkinter遇到问题

来自分类Dev

“ .gitignore”遇到问题

来自分类Dev

chmod遇到问题

来自分类Dev

DispatcherTimer遇到问题

来自分类Dev

Libtcodpy遇到问题

来自分类Dev

Android-在App Client中管理服务器端点时遇到问题

来自分类Dev

在Android测试中无法从资源转换为位图时遇到问题

来自分类Dev

我在带有espeak库的android设备上的pydroid 3中遇到问题