我有一个简单的表,它动态地添加TableRow
和删除TableRow
。我的要求是保持表格的第一行不变,所以我需要使用该removeView(star,end);
方法,但它似乎无法正常工作并抛出NullPointerException
. 我还使用了 removeAllViews() 方法,它工作正常,所以我不明白为什么它会抛出 NullPointerException!我该如何解决我的问题?这是我的代码:--
TableLayout table = (TableLayout) findViewById(R.id.myTable);
table.removeViews(1, table.getChildCount());
//table.removeAllViews();
table.postInvalidate();
bindData();
这是 logcat 错误:-
10-22 06:16:05.204: E/AndroidRuntime(828): Uncaught handler: thread main exiting due to uncaught exception
10-22 06:16:05.214: E/AndroidRuntime(828): java.lang.NullPointerException
10-22 06:16:05.214: E/AndroidRuntime(828): at android.view.ViewGroup.removeViewsInternal(ViewGroup.java:2116)
10-22 06:16:05.214: E/AndroidRuntime(828): at android.view.ViewGroup.removeViews(ViewGroup.java:2064)
10-22 06:16:05.214: E/AndroidRuntime(828): at com.techdeedapps.diamond.ViewStockActivity$BackgroungLoadingDiamondList.onPostExecute(ViewStockActivity.java:348)
10-22 06:16:05.214: E/AndroidRuntime(828): at com.techdeedapps.diamond.ViewStockActivity$BackgroungLoadingDiamondList.onPostExecute(ViewStockActivity.java:1)
10-22 06:16:05.214: E/AndroidRuntime(828): at android.os.AsyncTask.finish(AsyncTask.java:417)
10-22 06:16:05.214: E/AndroidRuntime(828): at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-22 06:16:05.214: E/AndroidRuntime(828): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-22 06:16:05.214: E/AndroidRuntime(828): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 06:16:05.214: E/AndroidRuntime(828): at android.os.Looper.loop(Looper.java:123)
10-22 06:16:05.214: E/AndroidRuntime(828): at android.app.ActivityThread.main(ActivityThread.java:4363)
10-22 06:16:05.214: E/AndroidRuntime(828): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 06:16:05.214: E/AndroidRuntime(828): at java.lang.reflect.Method.invoke(Method.java:521)
10-22 06:16:05.214: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-22 06:16:05.214: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-22 06:16:05.214: E/AndroidRuntime(828): at dalvik.system.NativeStart.main(Native Method)
它可以完美地使用这种方法删除所有视图。table.removeAllView()
那么为什么它不能使用这种方法table.removeViews(1,table.getChildCount()
呢?