views:

174

answers:

2

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

A: 

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
@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
@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
@CommonsWare: Also if this TabsActivity is the startup activity, the problem is the same. Do you have a working code sample?
xpepermint
@CommonsWare: this is MyParentActivity.this btw.
xpepermint
@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
@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
@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
A: 

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
Hum... example?
xpepermint
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