XmlPullAttributes 不能转换为 XmlBlockParser [英] XmlPullAttributes cannot be cast to XmlBlockParser

查看:62
本文介绍了XmlPullAttributes 不能转换为 XmlBlockParser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要查看的自定义 xml 文件

这是上述问题产生的错误

This is an error produced from the above question

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

I managed to get the XML file saved into the assets folder, and loaded (and I can even navigate through the different parts of the parser and produce outputs from them) but when I try to pass the XmlPullParser to LayoutInflater I get the above error. I am not totally sure why it is happening though.

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

Below is the activity that I am using (the part of it that involves parsing the Xml file) as well as the xml file (called activity_main.sv) and the logcat.

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

Ignore the file extension of the xml file. I simply renamed it because the IDE was having fits about inclusion. If the xml file is saved in the res/layouts folder it works just fine (I know because I copied it directly out of the res/layouts folder).

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");
     }
}

我的基本相对布局保存为activity_main.sv"

And my basic relativelayout saved as "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 输出

And finally the logcat output

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) 语句后.我现在有一个不同的错误.

EDIT After adding the factory.setValidating(true) statement. I have a different error now.

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)

推荐答案

出厂

factory.setValidating(true);

这篇关于XmlPullAttributes 不能转换为 XmlBlockParser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆