我正在尝试使用XmlPullParser从xml文档中获取一些数据。当我试图运行应用程序时由于空对象引用而崩溃。代码和错误如下,但我没有看到代码有任何问题。
public List<Tara> parseXML(Context context){
try{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
InputStream stream = context.getResources().openRawResource(R.raw.data_info);
xpp.setInput(stream, null);
int eventType = xpp.getEventType();
while( eventType != XmlPullParser.END_TAG){
if(eventType == XmlPullParser.START_TAG){
handleStartTag(xpp.getName());
} else if (eventType == XmlPullParser.END_TAG){
tagCurent = null;
} else if(eventType == XmlPullParser.TEXT){
handleText(xpp.getText());
}
eventType = xpp.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return listaTara;
}
似乎就行了
InputStream stream = context.getResources().openRawResource(R.raw.data_info);
显示下一个错误:
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: FATAL EXCEPTION: main
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: Process: com.example.ionut.simi_word_info, PID: 7539
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ionut.simi_word_info/com.example.ionut.simi_word_info.views.allCountry_activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2546)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:177)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5942)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.content.ContextWrapper.getResources(ContextWrapper.java:90)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at com.example.ionut.simi_word_info.helps.CountryPullParser.parseXML(CountryPullParser.java:31)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at com.example.ionut.simi_word_info.views.allCountry_activity.<init>(allCountry_activity.java:35)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at java.lang.reflect.Constructor.newInstance(Native Method)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at java.lang.Class.newInstance(Class.java:1650)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2536)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:177)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5942)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
感谢您的帮助!
答案 0 :(得分:0)
at com.example.ionut.simi_word_info.views.allCountry_activity。&lt; init&gt;(allCountry_activity.java:35)
不要对Context
构造函数中的Activity
执行任何操作。在系统初始化活动后,在onCreate(Bundle)
中执行此操作。
事实上,避免重写活动构造函数,它很少有意义。