onCheckedChangeListener无法使用Android Studio

时间:2014-10-25 18:51:40

标签: java android abstract-class

我一直在关注NerdRanch Android教程[第8章,Wiring Widgets]并遇到了这个麻烦的错误。这是我收到的错误:

  

Class'从OnCheckedChangeListener派生的匿名类'必须   要么被宣布为抽象,要么实现抽象方法   'onCheckedChange(CompoundButton,boolean)'中   'OnCheckedChangeListener'

这是我的CrimeFragment.java的代码

package com.example.justin.criminalintent;

// imports...

import static android.widget.CompoundButton.OnCheckedChangeListener;

public class CrimeFragment extends Fragment {
    private Crime mCrime;
    private EditText mTitleField;
    private Button mDateButton;
    private CheckBox mSolvedCheckBox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCrime = new Crime();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_crime, parent, false);

        mTitleField = (EditText)v.findViewById(R.id.crime_title);
        mTitleField.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(
                    CharSequence c, int start, int before, int count) {
                mCrime.setTitle(c.toString());
            }

            public void beforeTextChanged(
                    CharSequence c, int start, int count, int after) {
                //This space intentionally left blank
            }

            public void afterTextChanged(Editable c) {
                // This one too
            }
        });

        //Get a reference to the new button, set its text as date of the crime
        mDateButton = (Button)v.findViewById(R.id.crime_date);
        mDateButton.setText(mCrime.getDate().toString());
        mDateButton.setEnabled(false);

        //Get a reference to the checkbox, and set a listener that will update
        // the mSolved field of the CRIME
        mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
        mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChange(CompoundButton buttonView, boolean isChecked) {
                //Set the crime's solved property
                mCrime.setSolved(isChecked);
            }
        });

        return v;
    }
}

以下是Gradle Build的日志:

C:\Users\Justin\Desktop\CriminalIntent\app\src\main\java\com\example\justin\criminalintent\CrimeFragment.java
Error:(65, 82) error: <anonymous com.example.justin.criminalintent.CrimeFragment$2> is not abstract and does not override abstract method onCheckedChanged(CompoundButton,boolean) in OnCheckedChangeListener
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED

有关如何解决此错误的任何想法?

1 个答案:

答案 0 :(得分:4)

更改行

 public void onCheckedChange(CompoundButton buttonView, boolean isChecked) {

 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {