Why am I getting a null pointer exception from a TabWidget?

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.

+4
source share
4 answers

This works for me:

 tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("upcoming").setIndicator( getResources().getString(R.string.upcoming)).setContent(R.id.cell1)); 

Partial XML

 <?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:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <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"> <include android:id="@+id/cell1" layout="@layout/my_layout_file" /> <!-- another include for cell2, and the closing tags --> 
+1
source

For those of you who use TabHost not inside TabActivity, then be sure to call TabHost.setup (). Here is the documentation for it: http://developer.android.com/reference/android/widget/TabHost.html#setup () Failure to do this will throw a NullPointerException when trying to add tabs to your tab.

The textbook does not mention this, so I thought I brought it to your attention in order to save you guys while I was looking for them.

+20
source

Just wanted to introduce some idea based on statements from here:

http://code.google.com/p/android/issues/detail?id=14929

TabWidget crashes if you try to draw it before loading data. To prevent this, simply use your asynchronous tasks / handlers not only to load data, but also to set content. Of course, you could use some other view or progress dialog during the data loading process, just don't save the tab holder for now.

0
source

YEAR! You need to call:

TabHost.setup ()

before calling the method. if not, TabHost TabWidget should be null. I have made this mistake for so long.

0
source

Source: https://habr.com/ru/post/1311386/


All Articles