【发布时间】:2016-01-22 02:18:47
【问题描述】:
代码如下:
上下文 c = getContext().createPackageContext("com.master.schedule", Context.CONTEXT_INCLUDE_CODE|Context.CONTEXT_IGNORE_SECURITY);
int id = c.getResources().getIdentifier("layout_main", "layout","com.master.schedule"); LayoutInflater inflater = LayoutInflater.from(c); piflowView = inflater.inflate(id, null);
com.master.schedule 是我的包项目。上面的代码显示了另一个项目(他的包名称与我的不同)如何膨胀我的项目,在我的项目中只有一个 ViewGroup(我没有活动);当我调用“context.getApplicationContext”时,它返回null...我的项目代码如下:
public class CascadeLayout extends RelativeLayout {
private Context context;
public CascadeLayout(Context context) {
super(context);
this.context=context.getApplicationContext();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//here: context == null;
}
}
我发现createPackageContext() 给我的“Context c”是ContextImpl 类型;我认为这是导致返回 null;
那么我怎样才能得到一个不为空的ApplicationContext呢?
BTW:请不要劝我不要调用getApplicationContext();因为我必须使用.jar,jar需要在其中调用getApplicationContext();
非常感谢。
【问题讨论】:
-
你在调用什么对象
getApplicationContext()?你也在你的代码中使用了错误的包...... -
原谅我的英文表达,我已经更新了我的问题,我的 context.getApplicationContext();谢谢!