Android已弃用的方法

时间:2015-03-29 21:37:54

标签: java android

我尝试为getDrawable()添加两个方法,因为不推荐使用此方法。 怎么了?

    public class Misc {

    @TargetApi(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    public static Drawable getDrawable(Context context, int resource) {
        return context.getResources().getDrawable(resource, null);
    }

    @TargetApi(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
    public static Drawable getDrawable(Context context, int resource) {
        return context.getResources().getDrawable(resource);
    }

}



Duplicate method getDrawable(Context, int) in type Misc line 11 Java Problem
Duplicate method getDrawable(Context, int) in type Misc line 16 Java Problem

4 个答案:

答案 0 :(得分:2)

您应该使用支持库中的以下代码:

glfwWindowHint(GLFW_DEPTH_BITS, 64);

使用此方法相当于调用:

ContextCompat.getDrawable(context, R.drawable.***)

答案 1 :(得分:1)

您不能拥有两个具有相同签名的方法(相同的方法名称,相同的参数等)。

将其重写为:

public class Misc {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static Drawable getDrawable(Context context, int resource) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          return context.getResources().getDrawable(resource, null);
        }

        return context.getResources().getDrawable(resource);
    }

}

答案 2 :(得分:0)

错误是您错误地使用了overload!具体来说,不允许2个方法具有相同的名称和相同的输入参数。

答案 3 :(得分:0)

因为你已经定义了两次。

public static Drawable getDrawable(Context context, int resource) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return context.getResources().getDrawable(resource);
    } else {
        return context.getResources().getDrawable(resource, null);
    }
}

应该解决你的问题。