@Override 注释错误(android 首选项) [英] @Override annotation error (android prefs)

查看:26
本文介绍了@Override 注释错误(android 首选项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用此代码在我的应用中启用首选项时

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);
                    }
                }
            });
        };
    });
}
}

此行产生错误

 public void onCheckedChanged(CompoundButton compoundButton,boolean test) {

说:

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 

如果我删除了 @Override 注释,那么代码将不起作用,并且警告告诉我该部分代码未在本地使用.

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

谢谢

推荐答案

这是一个实现的事情.在 Java 5 与 Java 6 中,他们更改了是否可以对接口使用覆盖"(因为覆盖似乎意味着您正在覆盖某种默认行为,而您没有使用接口!).如果您愿意,可以在 Eclipse 首选项中搜索并将其从编译错误更改为编译警告.您在 CheckBox() 函数内部编写的代码对我来说看起来不错.

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.

但是,您永远不会调用 CheckBox 函数,因此这就是未在本地使用"错误的来源.您是否打算从 OnPreferenceClick 方法中调用该函数?

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?

这篇关于@Override 注释错误(android 首选项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆