Android动态添加行到表空例外

时间:2013-02-20 18:06:34

标签: java android nullpointerexception android-tablelayout

我正在尝试在运行时向表中添加一行,但在打印NullPointerException后会抛出1。代码在Activity中,我使用布尔标志active(在onCreate()方法中设置为true)以确保已实例化Activity。我认为异常可以引用的唯一事情是this,但我确保该类已被实例化。有人可以说出了什么问题吗?

if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.println("adding row...");

Element element = (Element) node;

System.out.println("1");

//Create the a row and add two text views
TableRow row2 = new TableRow(this);

System.out.println("2");

TextView text3 = new TextView(this);

System.out.println("3");

text3.setText("+"/*element.getElementsByTagName("pickup").item(0).getTextContent()*/);

System.out.println("4");

TextView text4 = new TextView(this);

text4.setText("+"/*element.getElementsByTagName("destination").item(0).getTextContent()*/);

text4.setGravity(android.view.Gravity.RIGHT);

System.out.println("5");

row2.addView(text3);

System.out.println("6");

row2.addView(text4);

System.out.println("7");

tableLayout.addView(row2, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

System.out.println("added row!");
}

堆栈追踪:

02-20 17:45:35.221: I/System.out(26702): adding row...
02-20 17:45:35.221: I/System.out(26702): 1
02-20 17:45:35.231: I/System.out(26702): BidActivity.xmlToTable() -         
java.lang.NullPointerException
02-20 17:45:35.231: W/System.err(26702): java.lang.NullPointerException
02-20 17:45:35.231: W/System.err(26702):    at     
android.content.ContextWrapper.getResources(ContextWrapper.java:89)
02-20 17:45:35.231: W/System.err(26702):    at     
android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
02-20 17:45:35.231: W/System.err(26702):    at android.view.View.<init>(View.java:3224)
02-20 17:45:35.231: W/System.err(26702):    at android.view.ViewGroup.<init>  
(ViewGroup.java:420)
02-20 17:45:35.241: W/System.err(26702):    at android.widget.LinearLayout.<init> 
(LinearLayout.java:168)
02-20 17:45:35.241: W/System.err(26702):    at android.widget.TableRow.<init>
(TableRow.java:61)
02-20 17:45:35.241: W/System.err(26702):    at   
com.example.taxidata.BidActivity.xmlToTable(BidActivity.java:129)
02-20 17:45:35.241: W/System.err(26702):    at     
com.example.taxidata.ConnectTask.parseMessage(ConnectTask.java:63)
02-20 17:45:35.241: W/System.err(26702):    at  
com.example.taxidata.ConnectTask.onProgressUpdate(ConnectTask.java:48)
02-20 17:45:35.241: W/System.err(26702):    at 
com.example.taxidata.ConnectTask.onProgressUpdate(ConnectTask.java:1)
02-20 17:45:35.241: W/System.err(26702):    at   
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
02-20 17:45:35.241: W/System.err(26702):    at  
android.os.Handler.dispatchMessage(Handler.java:99)
02-20 17:45:35.241: W/System.err(26702):    at android.os.Looper.loop(Looper.java:137)
02-20 17:45:35.241: W/System.err(26702):    at   
android.app.ActivityThread.main(ActivityThread.java:5039)
02-20 17:45:35.241: W/System.err(26702):    at    
java.lang.reflect.Method.invokeNative(Native Method)
02-20 17:45:35.241: W/System.err(26702):    at    
java.lang.reflect.Method.invoke(Method.java:511)
02-20 17:45:35.241: W/System.err(26702):    at   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-20 17:45:35.241: W/System.err(26702):    at    
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-20 17:45:35.241: W/System.err(26702):    at dalvik.system.NativeStart.main(Native  
Method)

0 个答案:

没有答案