Android将XML文件解析为arraylist(崩溃)

时间:2015-10-06 02:37:31

标签: android listview arraylist

(这已解决,代码中提到了解决方案)。这不是由空指针建议解决的。我是Android编程的新手,我正在试图弄清楚如何从文件中获取字符串并随机地在按钮上显示这些字符串。

目标是使用随机生成的问题(基于数据库)进行多项选择测试,并跟踪用户的正确和错误答案(但我远离那部分)。

我观看了一个xmlpull教程,该教程向我展示了如何拉出文档中每一行的一部分,然后将其列在屏幕上。我尝试调整它来填充一个arraylist,然后在列表视图中显示该arraylist,但是当我运行应用程序时,它崩溃了。我到处寻找,找不到任何告诉我我做错的事。这是XML的一行:

<root id="kanji">
<characters>
    <character kanji="一" english="one" on="ichi, itsu" kun="hito-tsu" grade="1"/>
</characters>

(编辑,添加布局XML):我的test_layout如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".Test">

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listone"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>
</RelativeLayout>

这是我的(更新的)代码:

public class Test extends Activity {

// This was incorrect: ArrayList kanjiList = new ArrayList();
// It should look like this:
ArrayList<String> kanjiList = new ArrayList<String>();
ListView lv;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_layout);

    try {
        kanjiList = getItemFromXML(this);
    } catch (XmlPullParserException e) {} catch (IOException e) {} {
        Toast.makeText(getApplicationContext(), "error parse xml", Toast.LENGTH_LONG).show();
    }
    lv = (ListView) findViewById(R.id.listone);
    // This is the array adapter, it takes the context of the activity as a
    // first parameter, the type of list view as a second parameter and your
    // array as a third parameter.
    ArrayAdapter < String > arrayAdapter = new ArrayAdapter < String > (
            this,
            android.R.layout.simple_list_item_1,
            kanjiList);

    lv.setAdapter(arrayAdapter);
}

public ArrayList getItemFromXML(Test test) throws XmlPullParserException, IOException {
    StringBuffer stringBuffer = new StringBuffer();
    Resources res = test.getResources();
    XmlResourceParser xpp = res.getXml(R.xml.database);
    xpp.next();
    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {
            if (xpp.getName().equals("character")) {
                stringBuffer.append(xpp.getAttributeValue(null, "kanji"));
//Added this line:
kanjiList.add(kanjiXpp.getAttributeValue(null, "kanji"));
            }
        }
        eventType = xpp.next();
    }
    return kanjiList;
}

}

以下是logcat的错误部分:

10-05 21:56:33.699  19039-19039/com.swordguy.kanjitest E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.swordguy.kanjitest/com.swordguy.kanjitest.Test}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
        at android.app.ActivityThread.access$600(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.app.Activity.findViewById(Activity.java:1794)
        at com.swordguy.kanjitest.Test.<init>(Test.java:77)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1319)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

你应该改变这一行

lv = (ListView) findViewById(android.R.id.list);

lv = (ListView) findViewById(R.id.list);

建议你的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
...
>

<ListView xmlns:android="http://schemas.android.com/apk/res/android" // you should remove this line. it isn't necessary here
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>
</RelativeLayout>

希望这个帮助