Android:无法加载XML

时间:2014-07-28 23:00:40

标签: android xml xml-parsing android-studio

新的Android Studio项目

创建资产文件夹

创建新活动的构造函数(项目中的第二个),以便在调用活动时读取XML。

将我想要读取的xml文件放在我创建的assets文件夹中

以下是读取xml

的代码
    AssetManager assetManager = getAssets();
    InputStream xmlFile= assetManager.open("menu.xml");

    // CREATE BUILDER FACTORY/BUILDER
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(xmlFile);

    //Element root = doc.getDocumentElement();
    //Log.d("root element",root.getNodeName());

    NodeList list = doc.getElementsByTagName("subcategory");

    Log.d("NODE", String.valueOf(list.getLength()));

运行应用程序时,会调用该按钮以显示新活动,但崩溃... 请帮忙

这是logcat输出

07-28 15:52:30.138    6990-6990/com.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app/com.app.MainMenu}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3691)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.content.ContextWrapper.getAssets(ContextWrapper.java:74)
            at com.app.MainMenu.<init>(MainMenu.java:60)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1409)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3691)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
            at dalvik.system.NativeStart.main(Native Method)

MainACtivity是一个默认的android studio活动,带有一个用于调用新活动的按钮 MainMenu是第二个调用XML的活动

请告知。

谢谢你!

1 个答案:

答案 0 :(得分:0)

我认为您需要为活动所在的任何上下文调用getAssets(),

context.getAssets();