I had a simple table and it add theTableRow and delete the TableRow dynamically. my requirement says to keep the first row of the table as it is heading so i need to use the removeView(star,end); method but it seems to b not working and throws the NullPointerException. i also used the removeAllViews() method, it working correctly so i cant understand why it throws the NullPointerException! how can i solve my problem?
this is my code:–
TableLayout table = (TableLayout) findViewById(R.id.myTable);
table.removeViews(1, table.getChildCount());
//table.removeAllViews();
table.postInvalidate();
bindData();
and this is the logcat errors :–
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)
it works perfectly on removing all view by using this method table.removeAllView() .then why it is not working on using this method table.removeViews(1,table.getChildCount() ?
Since you want to remove all items but the first one you need to make sure to only remove “number of child elements minus the last one”. I.e.
With the code line in your question you will try to remove one child view more than you actually have in your TableLayout.