我一直在关注NerdRanch Android教程[第8章,接线小部件],遇到了这个麻烦的错误。这是我收到的错误:
类“从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
关于如何解决此错误的任何想法?
最佳答案
换线
public void onCheckedChange(CompoundButton buttonView, boolean isChecked) {
至
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {