Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.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
Android 如何从另一个应用程序获取资源?_Android_Xml_Layout - Fatal编程技术网

Android 如何从另一个应用程序获取资源?

Android 如何从另一个应用程序获取资源?,android,xml,layout,Android,Xml,Layout,我发现了如何从另一个应用程序获取资源 我研究了很多关于这个的帖子 因此,提出了​​以下来源 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Resour

我发现了如何从另一个应用程序获取资源

我研究了很多关于这个的帖子

因此,提出了​​以下来源

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Resources themeR = null;
    try {
        themeR = getThemeResources("com.example.testskin01", this);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    int resID = themeR.getIdentifier("activity_main2" , "layout", "com.example.testskin01");

    View themeLayout = (View)themeR.getLayout(resID);

    setContentView(themeLayout);    
}

private static Resources getThemeResources(String themePackage, Context context) throws NameNotFoundException {
    try {
        return context.getPackageManager().getResourcesForApplication(themePackage);
    } catch (Exception e) {
        return null;
    }
}
我安装了“com.example.testskin01”包来测试设备。
使用“activity_main2.xml”

调用“resID”时,未发生错误

但下一行,称为来自的错误

"View themeLayout = (View)themeR.getLayout(resID);".
这是一条错误消息

"java.lang.ClassCastException: android.content.res.XmlBlock$Parser"
我怎样才能解决这个问题?请提出一些想法。

解决了

这是不正确的。
查看themeLayout=(查看)themeR.getLayout(resID)

这是正确的。
查看themeLayout=LayoutFlater.from(this).充气(themeR.getLayout(resID),null)