I am writing an android program in which I have an activity that uses tabs.
Activities
public class UnitActivity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost tabHost = getTabHost(); TabSpec spec; Resources res = getResources(); LayoutInflater.from(this).inflate(R.layout.unit_view, tabHost.getTabContentView(), true); spec = tabHost.newTabSpec("controls"); spec.setIndicator("Control", res.getDrawable(R.drawable.ic_tab_equalizer)); spec.setContent(R.id.txtview); tabHost.addTab(spec); }
}
XML referenced by R.layout.unit_view
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> <TextView android:id="@+id/txtview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="bottom" android:text="nullpointer this!" /> </FrameLayout> </LinearLayout> </TabHost>
As far as I can see, I am doing the same thing as in the tabs1 api tab example from the android sdk file. I tried "getLayoutInflator ()" instead of "LayoutInflator.from (this)" with the same result.
If I replaced the LayoutInflater line with "setContentView (R.layout.unit_view)", my program does not crash with an exceptional exception, but my content is completely empty and empty. I get a tab and hers.
I checked to make sure that R.layout.unit_view and tabHost are not null when it runs the LayoutInflater line, and it seems that everything is ok. They are deviated from zero. I also checked to make sure LayoutInflater.from (this) returns a valid layout fork object, and it does.
A log code indicating an error says
E / AndroidRuntime (541): java.lang.NullPointerException
E / AndroidRuntime (541): at android.widget.TabWidget.dispatchDraw (TabWidget.java:206)
E / AndroidRuntime (541): at android.view.ViewGroup.drawChild (ViewGroup.java:1529)
E / AndroidRuntime (541): at android.view.ViewGroup.dispatchDraw (ViewGroup.java:1258)
E / AndroidRuntime (541): at android.view.ViewGroup.drawChild (ViewGroup.java:1529)
E / AndroidRuntime (541): at android.view.ViewGroup.dispatchDraw (ViewGroup.java:1258)
E / AndroidRuntime (541): at android.view.ViewGroup.drawChild (ViewGroup.java:1529)
E / AndroidRuntime (541): at android.view.ViewGroup.dispatchDraw (ViewGroup.java:1258)
E / AndroidRuntime (541): at android.view.ViewGroup.drawChild (ViewGroup.java:1529)
E / AndroidRuntime (541): at android.view.ViewGroup.dispatchDraw (ViewGroup.java:1258)
E / AndroidRuntime (541): at android.view.ViewGroup.drawChild (ViewGroup.java:1529)
E / AndroidRuntime (541): at android.view.ViewGroup.dispatchDraw (ViewGroup.java:1258)
E / AndroidRuntime (541): at android.view.ViewGroup.drawChild (ViewGroup.java:1529)
E / AndroidRuntime (541): at android.view.ViewGroup.dispatchDraw (ViewGroup.java:1258)
E / AndroidRuntime (541): at android.view.View.draw (View.java:6538)
E / AndroidRuntime (541): at android.widget.FrameLayout.draw (FrameLayout.javahaps52)
E / AndroidRuntime (541): at android.view.ViewGroup.drawChild (ViewGroup.java:1531)
E / AndroidRuntime (541): at android.view.ViewGroup.dispatchDraw (ViewGroup.java:1258)
E / AndroidRuntime (541): at android.view.ViewGroup.drawChild (ViewGroup.java:1529)
E / AndroidRuntime (541): at android.view.ViewGroup.dispatchDraw (ViewGroup.java:1258)
E / AndroidRuntime (541): at android.view.View.draw (View.java:6538)
E / AndroidRuntime (541): at android.widget.FrameLayout.draw (FrameLayout.javahaps52)
E / AndroidRuntime (541): at com.android.internal.policy.impl.PhoneWindow $ DecorView.draw (PhoneWindow.java:1830)
E / AndroidRuntime (541): at android.view.ViewRoot.draw (ViewRoot.java:1349)
E / AndroidRuntime (541): at android.view.ViewRoot.performTraversals (ViewRoot.java:1114)
E / AndroidRuntime (541): at android.view.ViewRoot.handleMessage (ViewRoot.java:1633)
E / AndroidRuntime (541): at android.os.Handler.dispatchMessage (Handler.java:99)
E / AndroidRuntime (541): at android.os.Looper.loop (Looper.java:123)
E / AndroidRuntime (541): at android.app.ActivityThread.main (ActivityThread.java:4363)
E / AndroidRuntime (541): at java.lang.reflect.Method.invokeNative (Native Method)
E / AndroidRuntime (541): at java.lang.reflect.Method.invoke (Method.javaβ21)
E / AndroidRuntime (541): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:860)
E / AndroidRuntime (541): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:618)
E / AndroidRuntime (541): at dalvik.system.NativeStart.main (Native Method)
I / Process (61): Sending signal. PID: 541 SIG: 3
I / dalvikvm (541): threadid = 7: reacting to signal 3
I / dalvikvm (541): Wrote stack trace trace to '/data/anr/traces.txt'
Does anyone know how I can get this content on a tab without crashing my application? My actual program is more complicated and has more than one tab, but I simplified it before that, trying to find out why it crashes, but it still crashes, and I don't know why.
If I do not use LayoutInflator, my program does not crash, but I do not receive any content, just tabs.