ListPreference android 2.3.3 setIcon:NoSuchMethodError

时间:2014-05-23 11:17:04

标签: java android nosuchmethoderror listpreference

非常奇怪的错误

我在android上使用ListPreference。在使用android 2.3.3的三星gio s5660中使用setIcon()方法的时候,我得到了:

    05-23 14:14:31.169: E/AndroidRuntime(1572): FATAL EXCEPTION: main
05-23 14:14:31.169: E/AndroidRuntime(1572): java.lang.NoSuchMethodError: android.preference.ListPreference.setIcon
05-23 14:14:31.169: E/AndroidRuntime(1572):     at com.assignmentexpert.AssignmentPref.enableOnlyAssignmentProduct(AssignmentPref.java:2022)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at com.assignmentexpert.AssignmentPref.onResume(AssignmentPref.java:2003)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.app.Activity.performResume(Activity.java:3832)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:170)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:321)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:676)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.widget.TabHost.setCurrentTab(TabHost.java:348)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:141)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.view.View.performClick(View.java:2485)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.view.View$PerformClick.run(View.java:9080)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.os.Handler.handleCallback(Handler.java:587)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.os.Looper.loop(Looper.java:123)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at java.lang.reflect.Method.invoke(Method.java:507)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-23 14:14:31.169: E/AndroidRuntime(1572):     at dalvik.system.NativeStart.main(Native Method)

奇怪的是,当我在Nexus 7中启动它时,它可以正常工作。相同的代码库!!!

它有什么问题?

2 个答案:

答案 0 :(得分:1)

API 11中添加了setIcon方法,因此如果您尝试在API< 11中调用它,那么您将获得NoSuchMethodError

答案 1 :(得分:1)

你为我吝啬。好。但是最好给这个问题提供一些可用的解决方案。但是放置弊端更容易。

这是ListPreference的解决方案,也适用于较低的HoneyComb android:

public class IconPreference extends ListPreference {

private Drawable mIcon;

public IconPreference(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public IconPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs);
    setLayoutResource(R.layout.icon_pref);
    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.IconPreference, defStyle, 0);
    mIcon = a.getDrawable(R.styleable.IconPreference_icon);
}

@Override
public void onBindView(View view) {
    super.onBindView(view);
    ImageView imageView = (ImageView) view.findViewById(R.id.icon);
    if (imageView != null && mIcon != null) {
        imageView.setImageDrawable(mIcon);
    }
}

public void setIcon(Drawable icon) {
    if ((icon == null && mIcon != null) || (icon != null && !icon.equals(mIcon))) {
        mIcon = icon;
        notifyChanged();
    }
}

public Drawable getIcon() {
    return mIcon;
}

}