Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

@Override annotation error (android prefs)

When I was trying to use this code to enable preferences into my app

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;


public class Preferences extends PreferenceActivity {

private RadioButton btn01;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    btn01 = (RadioButton)findViewById(R.id.RadioButton01);
    Preference customPref = (Preference) findPreference("customPref");

    customPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){

        public boolean onPreferenceClick(Preference preference) {
            Toast.makeText(getBaseContext(),"The Custom Preference Has Been Clicked",Toast.LENGTH_LONG).show();
            SharedPreferences customSharedPreference = getSharedPreferences("myCutomSharedPrefs", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = customSharedPreference.edit();
            editor.putString("myCustomPref","The preference has been clicked");
            editor.commit();
            return true;
        }


        public void CheckBox() {
            final CheckBox ThisCheckBox = (CheckBox) findViewById (R.id.checkboxPref);
            ThisCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
                @Override
               public void onCheckedChanged(CompoundButton compoundButton,boolean test) {
                    if (ThisCheckBox.isChecked()){ 
                        btn01.setVisibility(0);
                    } else {
                        btn01.setVisibility(2);
                    }
                }
            });
        };
    });
}
}

An error is generated on this line

 public void onCheckedChanged(CompoundButton compoundButton,boolean test) {

saying:

Multiple markers at this line
- The method onCheckedChanged(CompoundButton, boolean) of type new 
 CompoundButton.OnCheckedChangeListener(){} must override a superclass method
- implements 
 android.widget.CompoundButton.OnCheckedChangeListener.onCheckedChanged 

If i remove the @Override annotation then the code doesn't work and a warning tells me that the that part of the code is not used locally.

Having run this past someone and baffling them I was wondering if you could help?

Are there any common scenarios that causes this error?

I thought it might be my project set up

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can go to Project settings --> Java Compiler, and switch the compiler compliance level to 1.6. I have to do it every time i import an existing project.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...