0

在我问我的问题之前,我会指出我对 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>

日志猫:

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)

编辑------- 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)
4

3 回答 3

0

你可以通过使用解决这个问题

context.getAssets();

或者,在您的情况下:

this.getAssets();
于 2013-12-19T15:26:15.877 回答
0

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

于 2013-08-01T13:22:42.600 回答
0

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

当你想创建新的活动时,你应该在你的 AndroidManifest.xml 中注册它。

于 2013-07-31T17:56:41.877 回答