【发布时间】: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声明)将有助于使您的问题更加清晰。 -
包含相关类的包名是什么?特别是,哪些包包含
MainActivity和FibuCommand?你在哪里调用FibuCommand.msg()方法? -
好的,我试着让它更简单,但我现在按照要求发布了整个流程!