0

我正在尝试在 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");
4

1 回答 1

2

assetManagernull

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

你可以使用AssetMangerActivity提供的。在这种情况下,可以分配它onCreate()

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

    this.assetManager = this.getAssets();

    ...
}

请参阅getAssets()文档。

于 2013-01-09T19:51:40.803 回答