作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在关注 NerdRanch Android 教程 [第 8 章,Wiring Widgets] 并遇到了这个麻烦的错误。这是我收到的错误:
Class 'Anonymous class derived from OnCheckedChangeListener' must either be declared abstract or implement abstract method 'onCheckedChange(CompoundButton, boolean)' in '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 构建的日志:
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) {
关于java - onCheckedChangeListener 不适用于 Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26565995/
我是一名优秀的程序员,十分优秀!