Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@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

like image 402
Jack Avatar asked Jan 21 '11 17:01

Jack


People also ask

What does @override mean in Android?

@Override is an java annotation . Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message. Also See.

What does @override annotation mean?

@Override @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. Overriding methods will be discussed in Interfaces and Inheritance.

What does @override mean in spring boot?

The @Override annotation denotes that the child class method overrides the base class method. For two reasons, the @Override annotation is useful. If the annotated method does not actually override anything, the compiler issues a warning. It can help to make the source code more readable.

Is @override necessary?

Not necessary, but is good programming practice. Allows you or anybody else who is reading your code to know that you have overridden a method/function.


3 Answers

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.

like image 57
0xf3f Avatar answered Oct 17 '22 13:10

0xf3f


It's an implementation thing. In Java 5 vs Java 6 they changed whether you could use "Override" with an interface (since Override seems to imply that you are overriding some sort of default behavior, which you are not doing with an interface!). If you so desire, you can search in the Eclipse preferences and change it from a compilation error to a compilation warning. You code inside of the CheckBox() function looks fine to me.

However, you are never calling the CheckBox function, so that's where the 'not used locally' error is coming from. Were you meaning to call that function from within the OnPreferenceClick method?

like image 26
Hamy Avatar answered Oct 17 '22 12:10

Hamy


just wanted to share, I wrote a post based on the info I found here. I hope it's helpful!

http://qtcstation.com/2011/05/android-and-jdk-compliance/

like image 5
Nelson Ramirez Avatar answered Oct 17 '22 12:10

Nelson Ramirez