我有一个带有以下代码的自定义 CheckBoxPreference,但它不适用于某些设备。我如何解决它?
我搜索了一些信息,但找不到相同的错误。
这是MCheckPreference类代码:
public class MCheckPreference extends CheckBoxPreference implements OnCheckedChangeListener, OnClickListener {
private TextView title;
private TextView sum;
private CheckBox toggleButton;
private LinearLayout mLinearItem;
public MCheckPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected View onCreateView(ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.check_preference, parent, false);
mLinearItem = (LinearLayout) view.findViewById(R.id.Item);
title = (TextView) view.findViewById(R.id.title);
sum = (TextView) view.findViewById(R.id.sum);
toggleButton = (CheckBox) view.findViewById(R.id.check);
title.setText(getTitle());
if (TextUtils.isEmpty(getSummary()) && TextUtils.isEmpty(getSummaryOn()) && TextUtils.isEmpty(getSummaryOff())) {
sum.setVisibility(View.GONE);
} else {
if (isChecked()) {
sum.setText(getSummaryOn());
} else
sum.setText(getSummaryOff());
if (TextUtils.isEmpty(getSummaryOff()) && TextUtils.isEmpty(getSummaryOn()))
sum.setText(getSummary());
sum.setVisibility(View.VISIBLE);
}
mLinearItem.setOnClickListener(this);
toggleButton.setChecked(isChecked());
toggleButton.setOnCheckedChangeListener(this);
return view;
}
@Override
public void onCheckedChanged(CompoundButton arg0, final boolean isChecked) {
setChecked(isChecked);
getOnPreferenceChangeListener().onPreferenceChange(this, isChecked);
}
@Override
public void onClick(View v) {
setChecked(!isChecked());
getOnPreferenceChangeListener().onPreferenceChange(this, isChecked());
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
}
这是LogCat:
java.lang.NullPointerException\
at android.preference.Injector$CheckBoxPreferenceHook.after_onBindView(Injector.java:23)\
at android.preference.CheckBoxPreference.onBindView(CheckBoxPreference.java:70)\
at android.preference.Preference.getView(Preference.java:464)\
at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221)\
at android.widget.AbsListView.obtainView(AbsListView.java:2296)\
at android.widget.ListView.makeAndAddView(ListView.java:1790)\
at android.widget.ListView.fillDown(ListView.java:691)\
at android.widget.ListView.fillFromTop(ListView.java:752)\
at android.widget.ListView.layoutChildren(ListView.java:1630)\
at android.widget.AbsListView.onLayout(AbsListView.java:2124)\
at android.view.View.layout(View.java:14830)\
at android.view.ViewGroup.layout(ViewGroup.java:4675)\
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)\
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)\
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)\
at android.view.View.layout(View.java:14830)\
at android.view.ViewGroup.layout(ViewGroup.java:4675)\
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)\
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)\
at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)\
at android.view.View.layout(View.java:14830)\
at android.view.ViewGroup.layout(ViewGroup.java:4675)\
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)\
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)\
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)\
at android.view.View.layout(View.java:14830)\
at android.view.ViewGroup.layout(ViewGroup.java:4675)\
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)\
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)\
at android.view.View.layout(View.java:14830)\
at android.view.ViewGroup.layout(ViewGroup.java:4675)\
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)\
at android.view.View.layout(View.java:14830)\
at android.view.ViewGroup.layout(ViewGroup.java:4675)\
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)\
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)\
at android.view.View.layout(View.java:14830)\
at android.view.ViewGroup.layout(ViewGroup.java:4675)\
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2077)\
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1834)\
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1090)\
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5769)\
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)\
at android.view.Choreographer.doCallbacks(Choreographer.java:574)\
at android.view.Choreographer.doFrame(Choreographer.java:544)\
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)\
at android.os.Handler.handleCallback(Handler.java:733)\
at android.os.Handler.dispatchMessage(Handler.java:95)\
at android.os.Looper.loop(Looper.java:136)\
at android.app.ActivityThread.main(ActivityThread.java:5017)\
at java.lang.reflect.Method.invokeNative(Method.java)\
at java.lang.reflect.Method.invoke(Method.java:515)\
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:804)\
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)\
at dalvik.system.NativeStart.main(NativeStart.java)}
谢谢!