如何将这个 int 初始化为常量?

发布于 2025-01-02 12:10:31 字数 652 浏览 1 评论 0原文

我的软件需要经常更改图像。我不想重复查找资源编号,而是想创建一些常量来表示每个图像引用。以下是迄今为止我的尝试:

此尝试在启动时产生强制关闭。

private final int EMPTY = getResources().getIdentifier("dotted_circle", "resId", "en.deco.android.livehud");

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        .....
    }

此尝试返回错误“最终变量可能未初始化”。删除final允许它工作。

private final int EMPTY

        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            EMPTY = getResources().getIdentifier("dotted_circle", "resId", "en.deco.android.livehud");
        }

首先在这里使用常量是一个好主意吗?

My software involves changing images a lot. Rather than look up the resource number repeatedly, I would like to create some constants to represent each image reference. Here are my attempts so far:

This attempt produces a force close on start up.

private final int EMPTY = getResources().getIdentifier("dotted_circle", "resId", "en.deco.android.livehud");

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        .....
    }

This Attempt returns an error "final variables may not be initialized". Removing final allows it to work.

private final int EMPTY

        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            EMPTY = getResources().getIdentifier("dotted_circle", "resId", "en.deco.android.livehud");
        }

Is using constants here a good idea in the first place?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

朱染 2025-01-09 12:10:31

首先在这里使用常量是个好主意吗?

不。最好是从资源中读取值的例程:

public class ImageUtils {

   private static int empty = -1;

   public static int getEmpty(Context context) {
        if(empty == -1)
          empty = context.getResources().getIdentifier("dotted_circle", "resId", "en.deco.android.livehud");

        return empty;
   }

}

Is using constants here a good idea in the first place?

No. It's better a routine that read the value from resources:

public class ImageUtils {

   private static int empty = -1;

   public static int getEmpty(Context context) {
        if(empty == -1)
          empty = context.getResources().getIdentifier("dotted_circle", "resId", "en.deco.android.livehud");

        return empty;
   }

}
风透绣罗衣 2025-01-09 12:10:31

不可能将资源加载到常量中。

我已经对这个主题进行了搜索并询问了 问题在这里 - 不可能。

这么看——它们已经是常数了。所以,如果有效性不是问题的话,您可以将它们放在我的网站上。或者将它们加载到变量中。很伤心,不是吗?

It is impossible to load resources into constants.

I had already made a search on the subject and asked a question here on it - No way.

Look at it so - they are already constants. so, you could you them i site, if effectivity is not a problem. Or load them into variables. It is sad, isn't it?

や三分注定 2025-01-09 12:10:31

第一个不起作用,因为您试图在构造类之前调用​​类的方法。第二个不起作用,因为您没有在构造时初始化最终变量。 (它需要在定义或构造函数中完成)。让您的 getResource 操作成为另一个类的静态方法(例如 ResourceUtilities.getResources()),这应该可以使其工作。

The first one doesn't work because you are trying to call methods of your class before it is constructed. The second doesn't work because you are not initialization your final variable on construction. (It needs to be done at the definition, or in the constructor). Make your getResource operation a static method of another class (something like ResourceUtilities.getResources()) and that should make it work.

娜些时光,永不杰束 2025-01-09 12:10:31

创建应用程序类。请参阅示例此处。
然后像这样使用它:

private final int EMPTY = App.getContext().getResources().getIdentifier("dotted_circle", "resId", "en.deco.android.livehud");

Create Application class. See example here.
Then use it like this:

private final int EMPTY = App.getContext().getResources().getIdentifier("dotted_circle", "resId", "en.deco.android.livehud");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文