2014-02-19 157 views
0

我在setOnCheckedChangeListener和OnCheckedChangeListener下有紅線。該錯誤消息告訴我:多個標記在這條線 - OnCheckedChangedListener不能被解析爲一個類型 - 在類型RadioGroup中的方法setOnCheckedChangedListener(RadioGroup.OnCheckedChangeListener)不適用於參數(新OnCheckedChangeListener(){})錯誤:OnCheckedChangeListener無法解析爲類型

package com.lifematters; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.Toast; 

public class Test1 extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.test1); 

    //define Navigation Image Buttons 
    final Button nextBtn = (Button) findViewById(R.id.btnNext); 



    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioAnswers1); 
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged (RadioGroup group, int checkedId){ 

     final RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioA1); 
     final RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioA2); 
     final RadioButton radioButton3 = (RadioButton) findViewById(R.id.radioA3); 
     final RadioButton radioButton4 = (RadioButton) findViewById(R.id.radioA4); 

     if (radioButton1.isChecked()) { 
      DisplayToast("No, not at all"); 

     } else if (radioButton2.isChecked()) { 
      DisplayToast("On some days"); 

     }else if (radioButton3.isChecked()) { 
      DisplayToast("On more than half the days"); 

     }else { 
      DisplayToast("Nearly everyday"); 
     } 
    } 

    }); 


    } 

} 
+0

按** ctrl + shift + o **在你的eclipse中導入類.. –

回答

4

兩種可能性:

  • 更改爲RadioGroup.OnCheckedChangeListener

  • 添加到您的類的開始:

    import android.widget.RadioGroup.OnCheckedChangeListener; 
    

至於DisplayToast,添加以下內容:

public void displayToast(String text){ 
    Toast.makeText(this, text, Toast.Toast.LENGTH_SHORT).show(); 
} 

而在你的代碼更改DisplayToastdisplayToast。方法名稱應該按照慣例以小寫字母開頭。只有類名應該以大寫字母開頭。

+0

修復了這個問題,謝謝:)。現在我得到DisplayToast的錯誤? DisplayToast(String)方法對於新類型RadioGroup.OnCheckedChangeListener()是未定義的。{} –

+0

您似乎從某處複製了代碼並留下了一些部分。看到我編輯的答案。 –

+0

非常感謝你,你有兩次Toast,但我解決了問題,謝謝:) –

3

您需要導入

import android.widget.RadioGroup; 
import android.widget.RadioGroup.OnCheckedChangeListener; 
1

導入其中兩個類/接口。

import android.widget.RadioGroup; 
import android.widget.RadioGroup.OnCheckedChangeListener; 

此代碼可以幫助OnChacked改變

  • //buttonView.getId()用於獲取按鈕
  • //boolean isChecked的IDS使用該按鈕被選中或不

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    
    
    if (isChecked) { 
    
        switch (buttonView.getId()) { 
    
        case R.id.radioA1: 
          DisplayToast("No, not at all"); 
         radioA2.setChecked(false); 
         break; 
    
        case R.id.radioA2: 
         DisplayToast("On some days") 
         radioA1.setChecked(false); 
         break; 
    
           ..... 
           ..... 
    
    
        default: 
         break; 
        } 
    
    } 
    

    }

0

我有同樣的問題,解決這樣說:

只要改變

.setOnCheckedChangeListener(new OnCheckedChangeListener(){... 

.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){... 

,這將是工作!

相關問題