【问题标题】:How to reference a string resource of a library from inside the library如何从库内部引用库的字符串资源
【发布时间】:2016-02-11 01:59:00
【问题描述】:

我已阅读 [问题 15557913]: (How to reference a string in strings.xml of an Android library in code?) 和 How to reference a string from another package in a library using XML in Android?

但我仍然很困惑.... 如果我理解正确,应用程序的 R 和库的 R 被合并,因此即使在应用程序的上下文中使用库中的 R,我也应该得到我的资源....

(按照cmets,我现在贴出原始程序流程),所以我有

UtilsApp/app -in fact a test app for the utils
        /nohutils/../src/../Command.java collection of utils e.g. this class
                 /res

NohFibu/app 
           /src with the MainActivity e.g. context...
           /res
       /jfibu/src../FibuCommand extends Command library of utils building on nohutils
             /res

MainActivity:

package com.nohkumado.nohfibu;
import com.nohkumado.nohfibu.R;

public class MainActivity extends Activity implements MsgR2StringI
{
  ....
  ShellI console = null;
  ....
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    ....
    if (console == null)  console = new Shell(this);
    else console.setContext(this);
    ....

    HashMap<String,CommandI> cmds = new HashMap<String,CommandI>();
    cmds.put("1", new EditJrlCmd(console));
    cmds.put("2", new EditKplCmd(console));
    ... etc ... 

    @Override
    public String msg(int stringid)
    {
     try
     {  
        return(getResources().getString(stringid));
     }
     catch (Resources.NotFoundException e)
     { Log.e(TAG, "not found message : " + stringid);}
     return("MSGNOTFOUND");
    }
    ...

现在我在 FibuCommand 中有一个方法:

package com.nohkumado.jfibu.commands;
public class FibuCommand extends Command 
{
  public FibuCommand(ShellI s)
  {
    super(s);
  }// public EditJrlCmd()
  public String msg(int resourceId)
  {
    MsgR2StringI msger = (MsgR2StringI) shell.get("msger");
    return(msger.msg(resourceId));      
  }// public String msg(String m)

最后:

package com.nohkumado.nohutils;
public class Command implements CommandI 
{
  ....
  protected ShellI shell = null;
  ...
  public Command(ShellI s)
  {
    shell = s;
  }// public Command()

package com.nohkumado.nohutils;
public class Shell implements ShellI,OnEditorActionListener,OnKeyListener
{
  ....
  protected MsgR2StringI context = null;
  ....
  public Shell(MsgR2StringI c)
  {
    super();
    context = c;
    ....
  }// public Shell()

现在当我尝试调用 msg(R.string.nofibuobj);我的 IDE 告诉我“com.nohkumado.nohutils.R.string”的未知成员“nofibuobj”的错误 但如果我写 msg(com.nohkumado.jfibu.R.string.nofibuobj) IDE 停止抱怨,但在 exe 时间我得到一个丑陋的 MSGNOTFOUND ....

但是在com.nohkumado.nohfibu包的上下文中,在com.nohkumado.jfibu.commands包中调用了msg方法,确实该方法继承自com.nohkumado.nohutils包的类。

我只想要一个包 com.nohkumado.jfibu (和子包)类来访问 com.nohkumado.jfibu.R.string 中的资源....如何实现呢?

提前致谢! 乙

【问题讨论】:

  • context 在哪里声明(你在msg() 中使用的那个)以及它是如何初始化的?
  • context 是 MainActivity 的引用,在每个 FibuCommand 的构造函数中传递,在 MainActivity::onCreate 方法中,我有一个循环实例化所有这些对象并自行传递它们。
  • 包含context 的声明和FibuCommand 的构造函数(以及周围的class 声明)将有助于使您的问题更加清晰。
  • 包含相关类的包名是什么?特别是,哪些包包含MainActivityFibuCommand?你在哪里调用FibuCommand.msg() 方法?
  • 好的,我试着让它更简单,但我现在按照要求发布了整个流程!

标签: java android


【解决方案1】:

好的, 我没有将 msg 方法放在 FibuCommand 中,而是将其放入应用程序的 MainActivity 中,并且它可以工作.... 不能说为什么

为了缩短调用时间,我必须明确导入库项目的 R,即使文档说我不应该这样做,但这是摆脱完全限定命名的唯一方法......

【讨论】:

    【解决方案2】:

    “问题”是MainActivityFibuCommand 位于不同的Java 包中。我建议你了解包和import 声明。

    请注意,还有另一个名为 R 的类。这通常与MainActivity 在同一个包中。确切的包名称在AndroidManifest.xml 中声明为&lt;application&gt; 标记中的package 属性,或者在build.gradle 中声明为defaultConfig 中的applicationId。每当您在与此根包不同的包中创建类时,您必须为R 提供import 语句。这实际上与使用来自不同包的任何其他类没有什么不同。

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多