来自Assets的getAssets NullPointerException XML

时间:2013-07-31 17:48:47

标签: android xml-parsing nullpointerexception

在我提出问题之前,我会指出我对android编程很陌生。 所以,我正在尝试解析资产文件夹中的xml文件中的数据,并将数据放在自定义列表视图中。我一直在寻找如何正确解析文件以及如何在几天内没有得到NullPointerException。如果这里真正的问题是我试图编码是可怕的,这是一个可能的解决方案。对我无能的任何部分的任何帮助将不胜感激。 代码:

 public class Z_PullMain extends ListActivity {

AssetManager manager = getAssets();

private ListView lv;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_specials_weekday_list);
    lv = (ListView) findViewById(android.R.id.list);
    Context context=getApplicationContext();
    ArrayList<HashMap<String, String>> listofspec = new ArrayList<HashMap<String, String>>();
    String lowercaseday = ((GlobalVariables) getApplication()).getlowercaseday();
    HashMap<String, String> specmap = new HashMap<String, String>();

    try {
        InputStream stream = context.getAssets().open("bar2.xml");
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(stream, "UTF-8");

    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
     if(eventType == XmlPullParser.START_TAG) {
         String tag_name=xpp.getName();
        if (tag_name==lowercaseday);{
            eventType = xpp.next();
            specmap.put("name", xpp.nextText().toString());
            xpp.next();
            specmap.put("price", xpp.nextText().toString());
            xpp.next();
            specmap.put("start", xpp.nextText().toString());
            xpp.next();
            specmap.put("end", xpp.nextText().toString());
            xpp.next();
            listofspec.add(specmap);
        }
     }
     eventType = xpp.next();
    }
    System.out.println("End document");
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    SimpleAdapter adapter = new SimpleAdapter(
            this,
            listofspec,
            R.layout.custom_row_view,
            new String[] {"name", "price", "start", "end"},
            new int[] {R.id.name,R.id.price, R.id.start, R.id.end}
            );
      setListAdapter(adapter);
}
}

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <thursday>
        <name>Everything</name>
        <price>1/2 Price</price>
        <start>10:00 PM</start>
        <end>12:00 AM</end>
    </thursday>
    <thursday>
        <name>Cover</name>
        <price>Free</price>
        <start>3:00 PM</start>
        <end>10:00 PM</end>
    </thursday>
    <friday>
        <name>Everything</name>
        <price>1/2 Price</price>
        <start>7:00 PM</start>
        <end>9:00 PM</end>
    </friday>
    <friday>
        <name>Coors Light Drafts</name>
        <price>$1.00</price>
        <start>3:00 PM</start>
        <end>12:00 AM</end>
    </friday>
    <saturday>
        <name>Everything</name>
        <price>1/2 Price</price>
        <start>7:00 PM</start>
        <end>9:00 PM</end>
    </saturday>
    <saturday>
        <name>Pinnacle</name>
        <price>$3.00</price>
        <start>3:00 PM</start>
        <end>12:00 AM</end>
    </saturday>
</root>

logcat的:

07-31 17:22:21.380: E/AndroidRuntime(2722): FATAL EXCEPTION: main
07-31 17:22:21.380: E/AndroidRuntime(2722): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.tronapps.barspecials/com.tronapps.barspecials.Z_PullMain}: java.lang.NullPointerException
07-31 17:22:21.380: E/AndroidRuntime(2722):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at android.os.Looper.loop(Looper.java:137)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at java.lang.reflect.Method.invoke(Method.java:511)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at dalvik.system.NativeStart.main(Native Method)
07-31 17:22:21.380: E/AndroidRuntime(2722): Caused by: java.lang.NullPointerException
07-31 17:22:21.380: E/AndroidRuntime(2722):     at android.content.ContextWrapper.getAssets(ContextWrapper.java:83)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at com.tronapps.barspecials.Z_PullMain.<init>(Z_PullMain.java:22)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at java.lang.Class.newInstanceImpl(Native Method)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at java.lang.Class.newInstance(Class.java:1319)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-31 17:22:21.380: E/AndroidRuntime(2722):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-31 17:22:21.380: E/AndroidRuntime(2722):     ... 11 more
07-31 17:23:14.881: E/Trace(2743): error opening trace file: No such file or directory (2)

EDIT ------- nullpointer异常是固定的,但列表为空。这是新的logcat

07-31 18:20:22.221: W/System.err(3145): org.xmlpull.v1.XmlPullParserException: precondition: START_TAG (position:TEXT (whitespace)@3:2 in java.io.InputStreamReader@410b6a48) 
07-31 18:20:22.221: W/System.err(3145):     at org.kxml2.io.KXmlParser.nextText(KXmlParser.java:2053)
07-31 18:20:22.231: W/System.err(3145):     at com.tronapps.barspecials.Z_PullMain.onCreate(Z_PullMain.java:49)
07-31 18:20:22.231: W/System.err(3145):     at android.app.Activity.performCreate(Activity.java:5104)
07-31 18:20:22.231: W/System.err(3145):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-31 18:20:22.241: W/System.err(3145):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-31 18:20:22.241: W/System.err(3145):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-31 18:20:22.241: W/System.err(3145):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-31 18:20:22.251: W/System.err(3145):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-31 18:20:22.251: W/System.err(3145):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 18:20:22.251: W/System.err(3145):     at android.os.Looper.loop(Looper.java:137)
07-31 18:20:22.251: W/System.err(3145):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-31 18:20:22.263: W/System.err(3145):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 18:20:22.271: W/System.err(3145):     at java.lang.reflect.Method.invoke(Method.java:511)
07-31 18:20:22.282: W/System.err(3145):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-31 18:20:22.282: W/System.err(3145):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-31 18:20:22.282: W/System.err(3145):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

请在AndroidManifest.xml中添加您的活动Z_PullMain

如果您想进行新活动,请在AndroidManifest.xml中注册。

答案 1 :(得分:0)

“如果这很明显,请确保您已将AssetManager manager = getAssets();更改为AssetManager manager;只是{{1}}道歉” - @Ken Wolf解决了我的NullPointer问题。

答案 2 :(得分:0)

您可以使用

解决此问题
context.getAssets();

或者,在您的情况下:

this.getAssets();