XmlPullAttributes无法强制转换为XmlBlockParser

问题描述 投票:6回答:3

Custom xml file to view

这是由上述问题产生的错误

我设法将XML文件保存到assets文件夹中,然后加载(我甚至可以浏览解析器的不同部分并从中生成输出)但是当我尝试将XmlPullParser传递给LayoutInflater时,我得到了上述错误。我不完全确定为什么会发生这种情况。

下面是我正在使用的活动(其中涉及解析Xml文件的部分)以及xml文件(称为activity_main.sv)和logcat。

忽略xml文件的文件扩展名。我只是重命名它,因为IDE适合包含。如果xml文件保存在res / layouts文件夹中,它可以正常工作(我知道因为我直接将它复制到res / layouts文件夹中)。

public void onCreate(Bundle savedInstanceState){
...
     try {
          XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
          XmlPullParser parser = factory.newPullParser();
          InputStream in = getResources().getAssets().open("views/activity_main.sv");
          parser.setInput(new InputStreamReader(in));
          LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        The below line is line 73, the line which is giving me issue
          View view = inflate.inflate(parser, null);
          setContentView(view);
     }catch(Exception e){
          e.printStackTrace();
//        Consider the below line Log.d("TAG","Well fuck me it didnt work"). AppLog is a custom class I created to help manage logging.
          AppLog.log("Well fuck me it didnt work");
     }
}

我的基本relativelayout保存为“activity_main.sv”

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

最后是logcat输出

10-24 19:36:55.273: W/EGL_emulation(4724): eglSurfaceAttrib not implemented
10-24 19:36:55.273: W/OpenGLRenderer(4724): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa34192a0, error=EGL_SUCCESS
10-24 19:47:15.252: W/System.err(8074): java.lang.ClassCastException: android.util.XmlPullAttributes cannot be cast to 

android.content.res.XmlBlock$Parser
10-24 19:47:15.253: W/System.err(8074):     at android.content.res.Resources$Theme.obtainStyledAttributes

(Resources.java:1483)
10-24 19:47:15.253: W/System.err(8074):     at android.content.Context.obtainStyledAttributes(Context.java:460)
10-24 19:47:15.253: W/System.err(8074):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
10-24 19:47:15.253: W/System.err(8074):     at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
10-24 19:47:15.253: W/System.err(8074):     at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
10-24 19:47:15.253: W/System.err(8074):     at pro.bladebeat.intentplayground.MainActivity.onCreate

(MainActivity.java:73)
10-24 19:47:15.253: W/System.err(8074):     at android.app.Activity.performCreate(Activity.java:5933)
10-24 19:47:15.253: W/System.err(8074):     at android.app.Instrumentation.callActivityOnCreate

(Instrumentation.java:1105)
10-24 19:47:15.253: W/System.err(8074):     at android.app.ActivityThread.performLaunchActivity

(ActivityThread.java:2251)
10-24 19:47:15.253: W/System.err(8074):     at android.app.ActivityThread.handleLaunchActivity

(ActivityThread.java:2360)
10-24 19:47:15.253: W/System.err(8074):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
10-24 19:47:15.253: W/System.err(8074):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
10-24 19:47:15.253: W/System.err(8074):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-24 19:47:15.253: W/System.err(8074):     at android.os.Looper.loop(Looper.java:135)
10-24 19:47:15.253: W/System.err(8074):     at android.app.ActivityThread.main(ActivityThread.java:5221)
10-24 19:47:15.253: W/System.err(8074):     at java.lang.reflect.Method.invoke(Native Method)
10-24 19:47:15.253: W/System.err(8074):     at java.lang.reflect.Method.invoke(Method.java:372)
10-24 19:47:15.253: W/System.err(8074):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run

(ZygoteInit.java:899)
10-24 19:47:15.253: W/System.err(8074):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-24 19:47:15.257: D/AppLog(8074): Well fuck me it didnt work

编辑添加factory.setValidating(true)语句后。我现在有一个不同的错误。

11-04 14:59:52.682: W/System.err(4664): org.xmlpull.v1.XmlPullParserException: unsupported feature: http://xmlpull.org/v1/doc/features.html#validation (position:START_DOCUMENT null@1:1) 
11-04 14:59:52.684: W/System.err(4664):     at org.kxml2.io.KXmlParser.setFeature(KXmlParser.java:2100)
11-04 14:59:52.684: W/System.err(4664):     at org.xmlpull.v1.XmlPullParserFactory.newPullParser(XmlPullParserFactory.java:135)
11-04 14:59:52.684: W/System.err(4664):     at pro.bladebeat.intentplayground.MainActivity.onCreate(MainActivity.java:36)
11-04 14:59:52.684: W/System.err(4664):     at android.app.Activity.performCreate(Activity.java:5933)
11-04 14:59:52.684: W/System.err(4664):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-04 14:59:52.684: W/System.err(4664):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
11-04 14:59:52.684: W/System.err(4664):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
11-04 14:59:52.684: W/System.err(4664):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-04 14:59:52.684: W/System.err(4664):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
11-04 14:59:52.684: W/System.err(4664):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-04 14:59:52.684: W/System.err(4664):     at android.os.Looper.loop(Looper.java:135)
11-04 14:59:52.684: W/System.err(4664):     at android.app.ActivityThread.main(ActivityThread.java:5221)
11-04 14:59:52.684: W/System.err(4664):     at java.lang.reflect.Method.invoke(Native Method)
11-04 14:59:52.684: W/System.err(4664):     at java.lang.reflect.Method.invoke(Method.java:372)
11-04 14:59:52.684: W/System.err(4664):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-04 14:59:52.684: W/System.err(4664):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
java android xml xml-parsing
3个回答
-1
投票

放在工厂之后

factory.setValidating(true);

5
投票

我知道这是一个老问题,但我想给那些想要使用这种方法或最近遇到问题的人提供答案。

我研究了大约五个小时的android源代码。 XmlBlock是一个私人课程,XmlBlock.Parser在其中,所以只能通过android.content.res的课程访问。如果你使用这个方法从方法Resources.getXml(id)获得xml它将没有任何问题(!),因为getXml调用creates a XmlBlock.Parser的函数,其特殊算法是为应用程序内部资源设计的。所以不要浪费你的时间让它工作(XmlResourceParser或类似的东西不能投到XmlBlock.Parser


4
投票

Doc LayoutInflate

看看这个:

重要由于性能原因,视图通胀在很大程度上依赖于在构建时完成的XML文件的预处理。因此,目前无法在运行时将LayoutInflater与XmlPullParser一起使用普通XML文件。

© www.soinside.com 2019 - 2024. All rights reserved.