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