Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从库内部引用库的字符串资源_Java_Android - Fatal编程技术网

Java 如何从库内部引用库的字符串资源

Java 如何从库内部引用库的字符串资源,java,android,Java,Android,我已阅读[问题15557913]:()及 但我还是很困惑。。。。 如果我理解正确,应用程序的R和库中的R会合并,因此即使在库中与应用程序的上下文一起使用R,我也应该获得我的资源 (在评论之后,我现在发布原始程序流程),因此 UtilsApp/app -in fact a test app for the utils /nohutils/../src/../Command.java collection of utils e.g. this class

我已阅读[问题15557913]:()及

但我还是很困惑。。。。 如果我理解正确,应用程序的R和库中的R会合并,因此即使在库中与应用程序的上下文一起使用R,我也应该获得我的资源

(在评论之后,我现在发布原始程序流程),因此

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
主要活动:

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");
    }
    ...
最后:

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

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

我只需要一个包com.nohkumado.jfibu(和子包)类来访问com.nohkumado.jfibu.R.string中的资源。。。。如何做到这一点

提前谢谢! B

好的, 我没有将msg方法放在FibuCommand中,而是将它放在应用程序的main活动中,它可以工作。。。。说不出为什么

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

问题在于
MainActivity
FibuCommand
在不同的Java包中。我建议您了解软件包和
import
语句


请注意,还有另一个名为
R
的类。这通常与
MainActivity
在同一个包中。确切的包名在
AndroidManifest.xml
中声明为
标记中的
package
属性,或在
build.gradle
中声明为
defaultConfig
中的
applicationId
。无论何时在不同于此根包的包中创建类,都必须为
R
使用
import
语句。这实际上与使用不同包中的任何其他类没有什么不同。

声明了
上下文
的位置(您在
msg()
中使用的一个)以及它是如何初始化的?上下文是MainActivity上的引用,在每个FibuCommand的构造函数中传递,在MainActivity::onCreate方法中,我有一个循环实例化所有这些对象并传递它们自己。包括
上下文
的声明和
FibuCommand
的构造函数(以及周围的
声明)将有助于让您的问题更清楚一点。包含所涉及类的包名是什么?特别是,哪些包包含
MainActivity
FibuCommand
?你在哪里调用
FibuCommand.msg()
方法?好的,我试着让它更简单,但根据要求,我现在发布了整个流程!
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()