How to disable a tab bar item in android?
-
13-06-2021 - |
Domanda
I am making my first android app. I want to disable a tab bat item in my android app. I searched and found the following way to do that:
tabHost = (TabHost)findViewById(R.id.tabhost);
tabHost.getTabWidget().getChildTabViewAt(your_index).setEnabled(false);
but its not working and i am getting following error:
06-11 16:53:15.927: D/AndroidRuntime(8466): Shutting down VM
06-11 16:53:15.927: W/dalvikvm(8466): threadid=1: thread exiting with uncaught exception (group=0x40028a00)
06-11 16:53:16.037: D/dalvikvm(8466): GC_FOR_MALLOC freed 5350 objects / 347632 bytes in 98ms
06-11 16:53:16.047: E/AndroidRuntime(8466): FATAL EXCEPTION: main
06-11 16:53:16.047: E/AndroidRuntime(8466): java.lang.IllegalStateException: Could not execute method of the activity
06-11 16:53:16.047: E/AndroidRuntime(8466): at android.view.View$1.onClick(View.java:2072)
06-11 16:53:16.047: E/AndroidRuntime(8466): at android.view.View.performClick(View.java:2408)
06-11 16:53:16.047: E/AndroidRuntime(8466): at android.view.View$PerformClick.run(View.java:8817)
06-11 16:53:16.047: E/AndroidRuntime(8466): at android.os.Handler.handleCallback(Handler.java:587)
06-11 16:53:16.047: E/AndroidRuntime(8466): at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 16:53:16.047: E/AndroidRuntime(8466): at android.os.Looper.loop(Looper.java:143)
06-11 16:53:16.047: E/AndroidRuntime(8466): at android.app.ActivityThread.main(ActivityThread.java:4914)
06-11 16:53:16.047: E/AndroidRuntime(8466): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 16:53:16.047: E/AndroidRuntime(8466): at java.lang.reflect.Method.invoke(Method.java:521)
06-11 16:53:16.047: E/AndroidRuntime(8466): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-11 16:53:16.047: E/AndroidRuntime(8466): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-11 16:53:16.047: E/AndroidRuntime(8466): at dalvik.system.NativeStart.main(Native Method)
06-11 16:53:16.047: E/AndroidRuntime(8466): Caused by: java.lang.reflect.InvocationTargetException
06-11 16:53:16.047: E/AndroidRuntime(8466): at com.eplinovo.runnoandroid.ActivityViewActivity.startClick(ActivityViewActivity.java:40)
06-11 16:53:16.047: E/AndroidRuntime(8466): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 16:53:16.047: E/AndroidRuntime(8466): at java.lang.reflect.Method.invoke(Method.java:521)
06-11 16:53:16.047: E/AndroidRuntime(8466): at android.view.View$1.onClick(View.java:2067)
06-11 16:53:16.047: E/AndroidRuntime(8466): ... 11 more
06-11 16:53:16.047: E/AndroidRuntime(8466): Caused by: java.lang.NullPointerException
06-11 16:53:16.047: E/AndroidRuntime(8466): ... 15 more
Why am i getting that? Thanks in advance.
Soluzione 2
I was not getting tabhost in right way.
tabHost = (TabHost)findViewById(R.id.tabhost);
Following is the right way to get tabhost from child activity:
tabHost = (TabHost)getParent().findViewById(android.R.id.tabhost);
Altri suggerimenti
Ok. Go to the top of the error in logcat, which is ActivityViewActivity.startClick. Go ahead and click on this line. It will take you to some line in startClick method where there is a null pointer exeption. Tell us what is on that line. It might be the line above. If it is than for example tabHost might be null. Check this separately.
tabHost.getWidget()
tagHost.getWidget().getChildTabViewAt(your_index) // I bet its this one thats null.
if any of the above are null you will get that exception assumming that the line causing the problem. If not its something similar on another line.
So divide and conquer. Find the line. Split it up to find what might be null. Then set a breakpoint before the line runs, and step one line at a time. Hover over to see what is null at each step. Thats how you can solve any null pointer in android.