0

Hey... i'm trying to create an activity with the layout structure like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TabHost android:id="@+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:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:id="@android:id/tabs" 
                /> 
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                >
            </FrameLayout>
        </LinearLayout>
     </TabHost>

     <some code here>

</LinearLayout>

What is wrong here? I'm getting nullPointerException in my activity

public class TabsActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);

        // Resources res = getResources();
        // TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
   }
}

The problem is with nesting. There is no problem with the TabHost as the main XML node. Thx!

Error: Screenshot

xpepermint
  • 35,055
  • 30
  • 109
  • 163

2 Answers2

0

You are misinterpreting your stack trace.

The exception is occurring inside Intent. The Intent you are using to start the activity is invalid. Fix your Intent, and your problem will go away.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @CommonsWare: Hum... no idea what could be wrong with it. Other activities work but they do not have TabHost component in the layout. How should I call it? I call it like this startActivity(new Intent(this, TabsActivity.class)); – xpepermint Sep 19 '10 at 12:36
  • @xpepermint: "I call it like this startActivity(new Intent(this, TabsActivity.class));" -- are you sure about that? That seems unlikely given your stack trace. You are creating your `Intent` from an inner class of your activity, so `this` is unlikely to be a `Context`. – CommonsWare Sep 19 '10 at 12:50
  • @CommonsWare: Also if this TabsActivity is the startup activity, the problem is the same. Do you have a working code sample? – xpepermint Sep 19 '10 at 12:50
  • @CommonsWare: this is MyParentActivity.this btw. – xpepermint Sep 19 '10 at 12:53
  • @xpepermint: Here are a pair of projects demonstrating `startActivity()`: http://github.com/commonsguy/cw-android/tree/master/Prefs/Dialogs/ http://github.com/commonsguy/cw-andtutorials/tree/master/12-Activities/ Also note that you need to populate your tabs in `onCreate()`, otherwise you will get errors. – CommonsWare Sep 19 '10 at 13:51
  • @CommonsWare: Hum... nope... my code is just like yours :). Do you have an example of TabHost inside LinearLayout layout? As I sad everything works but that special case. – xpepermint Sep 19 '10 at 14:10
  • @xpepermint: "Do you have an example of TabHost inside LinearLayout layout?" -- no. "As I sad everything works but that special case." -- as I said, that does not fit the stack trace you have shown. – CommonsWare Sep 19 '10 at 14:29
0

I had a similar problem and the solution was to get the TabHost and call setup(). Hard to say if that's your problem here.

noah
  • 21,289
  • 17
  • 64
  • 88
  • Ummm: findViewById(android.R.id.tabhost).setup(); Your screenshot doesn't show the whole trace, so it's hard to say if it's the same problem as mine. – noah Sep 27 '10 at 14:34