I tried to build a dynamic tablelayout by an XML file placed in the same folder as the activities coding file. However, I got the following null point exception. I suspect that it's caused by the path of the XML file as actually I am not sure if i could just place the file name in creating the file object. Could you advice me the possible cause of the error, please?
TableLayout layout = new TableLayout(this);
layout.setOrientation(TableLayout.VERTICAL);
layout.setStretchAllColumns(true);
//layout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
File xmlSrc = new File("menu.xml");
try {
stream = new FileInputStream(xmlSrc);
XMLParser menuParser = new XMLParser();
entries = menuParser.parse(stream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if (stream != null)
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
for (XMLParser.Entry entry : entries){
TableRow food_row = new TableRow(this);
TextView food_name = new TextView(this);
food_name.setText(entry.name);
TextView food_description = new TextView(this);
food_description.setText(entry.description);
food_row.addView(food_name);
food_row.addView(food_description);
layout.addView(food_row);
}
setContentView(layout);
Here is the exception I got
01-31 08:49:15.865 739-739/andapp.gt.littledan E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{andapp.gt.littledan/andapp.gt.littledan.CheckActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at andapp.gt.littledan.CheckActivity.onCreate(CheckActivity.java:63)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
0 comments:
Post a Comment