(This is solved, and the solutions are mentioned in the code). This was NOT solved by the null pointer suggestion. I'm very new to Android programming and I'm trying to figure out how to get strings from a file and display those strings on buttons randomly.
The goal is to make a multiple choice test with randomly generated questions (based on a database) and keep track of the user's correct and incorrect answers (but I'm far from that part).
I watched an xmlpull tutorial which showed me how to pull a section of every line in the document and then list it on screen. I tried to adapt it to populate an arraylist and then display that arraylist in a listview, but when I run the app, it crashes. I've looked everywhere and can't find anything telling me what I'm doing wrong. Here's a line of the XML:
<root id="kanji">
<characters>
<character kanji="一" english="one" on="ichi, itsu" kun="hito-tsu" grade="1"/>
</characters>
(Edit, added the layout XML): My test_layout is as follows:
<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>
And here's my (updated) code:
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;
}
}
The following is the error section of 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)