根据网上的这篇文章http://blog.csdn.net/singwhatiwanna/article/details/39937639,本人想从a.apk中动态加载b.apk中的一个Fragment,在a.apk中的Activity中已经重写了这三个方法
@Override
public AssetManager getAssets() {
if(mCurrentPackage == null ||
!mResources.containsKey(mCurrentPackage))
return super.getAssets();
else {
return mResources.get(mCurrentPackage).getAssetManager();
}
}
@Override
public Resources getResources() {
if(mCurrentPackage == null ||
!mResources.containsKey(mCurrentPackage))
return super.getResources();
else {
ResourceWarp wrapResourceWarp = mResources.get(mCurrentPackage);
return wrapResourceWarp.getResources();
}
}
@Override
public Theme getTheme() {
if(mCurrentPackage == null ||
!mResources.containsKey(mCurrentPackage))
return super.getTheme();
else {
ResourceWarp wrapResourceWarp = mResources.get(mCurrentPackage);
return wrapResourceWarp.getTheme();
}
根据当前需加载的包名,自动返回资源,但是在b.apk中的Fragment的方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle saveInstanceState) {
super.onCreateView(inflater, container, saveInstanceState);
View view = inflater.inflate(R.layout.test_appbase, container, false);
ImageView imgView = (ImageView)getView().findViewById(R.id.imageView1);
return view;
}
运行到ImageView imgView = (ImageView)getView().findViewById(R.id.imageView1);处会出错,无法找到R.id.imageView1,但是View view = inflater.inflate(R.layout.test_appbase, container, false);
这句代码又能通过
@Override
public AssetManager getAssets() {
if(mCurrentPackage == null ||
!mResources.containsKey(mCurrentPackage))
return super.getAssets();
else {
return mResources.get(mCurrentPackage).getAssetManager();
}
}
@Override
public Resources getResources() {
if(mCurrentPackage == null ||
!mResources.containsKey(mCurrentPackage))
return super.getResources();
else {
ResourceWarp wrapResourceWarp = mResources.get(mCurrentPackage);
return wrapResourceWarp.getResources();
}
}
@Override
public Theme getTheme() {
if(mCurrentPackage == null ||
!mResources.containsKey(mCurrentPackage))
return super.getTheme();
else {
ResourceWarp wrapResourceWarp = mResources.get(mCurrentPackage);
return wrapResourceWarp.getTheme();
}
根据当前需加载的包名,自动返回资源,但是在b.apk中的Fragment的方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle saveInstanceState) {
super.onCreateView(inflater, container, saveInstanceState);
View view = inflater.inflate(R.layout.test_appbase, container, false);
ImageView imgView = (ImageView)getView().findViewById(R.id.imageView1);
return view;
}
运行到ImageView imgView = (ImageView)getView().findViewById(R.id.imageView1);处会出错,无法找到R.id.imageView1,但是View view = inflater.inflate(R.layout.test_appbase, container, false);
这句代码又能通过
解决方案
10
无法找到? 确定布局文件里有没有
30
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle saveInstanceState) {
super.onCreateView(inflater, container, saveInstanceState);
View view = inflater.inflate(R.layout.test_appbase, container, false);
ImageView imgView = (ImageView) view.findViewById(R.id.imageView1);
return view;
}
ImageView imgView = (ImageView) view.findViewById(R.id.imageView1); 这里要用view.findViewById();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle saveInstanceState) {
super.onCreateView(inflater, container, saveInstanceState);
View view = inflater.inflate(R.layout.test_appbase, container, false);
ImageView imgView = (ImageView) view.findViewById(R.id.imageView1);
return view;
}
ImageView imgView = (ImageView) view.findViewById(R.id.imageView1); 这里要用view.findViewById();
60
虽然它们的类名称一样,查是他们的classloader不一样。理解这个需要对classloader有一定理解,下面这篇文章有讲到你这种情况,你可以看下。
http://www.alloyteam.com/2014/04/android-cha-jian-yuan-li-pou-xi/