我试图解析Android中的XML,但出现以下错误

01-10 01:17:19.050: W/System.err(3276): java.lang.NullPointerException
01-10 01:17:19.050: W/System.err(3276):     at com.m7.nomad.XMLParsingActivity.parseCategoryXML(XMLParsingActivity.java:31)
01-10 01:17:19.050: W/System.err(3276):     at com.m7.nomad.XMLParsingActivity.onCreate(XMLParsingActivity.java:26)
01-10 01:17:19.050: W/System.err(3276):     at android.app.Activity.performCreate(Activity.java:4465)
01-10 01:17:19.050: W/System.err(3276):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-10 01:17:19.050: W/System.err(3276):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
01-10 01:17:19.055: W/System.err(3276):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
01-10 01:17:19.055: W/System.err(3276):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-10 01:17:19.055: W/System.err(3276):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
01-10 01:17:19.055: W/System.err(3276):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 01:17:19.055: W/System.err(3276):     at android.os.Looper.loop(Looper.java:137)
01-10 01:17:19.055: W/System.err(3276):     at android.app.ActivityThread.main(ActivityThread.java:4507)
01-10 01:17:19.055: W/System.err(3276):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 01:17:19.055: W/System.err(3276):     at java.lang.reflect.Method.invoke(Method.java:511)
01-10 01:17:19.055: W/System.err(3276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-10 01:17:19.060: W/System.err(3276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-10 01:17:19.060: W/System.err(3276):     at dalvik.system.NativeStart.main(Native Method)




public class XMLParsingActivity extends SherlockActivity {

    AssetManager assetManager;

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

        parseCategoryXML();
    }

    private void parseCategoryXML() {
        try {
            InputStream is = assetManager.open("categories.xml");

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            CategoriesXmlHandler catXMLHandler = new CategoriesXmlHandler();

            xr.setContentHandler(catXMLHandler);
            InputSource inStream = new InputSource(is);
            xr.parse(inStream);
            is.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


错误指向此行

InputStream is = assetManager.open("categories.xml");

最佳答案

assetManagernull

您定义了变量assetManager,但是没有为其分配值(至少没有在您提供的代码中)。

您可以使用活动提供的AssetManger。在这种情况下,可以在onCreate()中分配它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.assetManager = this.getAssets();

    ...
}


请参阅getAssets()文档。

10-07 13:16