I\'ve tried the Tab Layout example, and I\'ve also fixed the few typos in the example (and added all the activities to the manifest). However, when I run it on the emulator
You should post some more of the code so that we can make better assumptions. Especially make sure you instantiate the tabHost Somewhere.
Just want to say thanks to all that posted here. Solved my problem (the same as everyone else here). It was very frustrating to get this to work. They should have really simplified the example much better.
To try to sum up the changes needed.
Move all 3 activity classes to separate files. example I ended up using for my SongsActivity.java file (with eclipse error suggestions)
package com.example.HelloTabWidget;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SongsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Songs tab");
setContentView(textview);
}
}
I was having the same issue. I started the tutorials yesterday and this was the only one to have problems.
The null pointer is thrown at this line the first time its called
tabHost.addTab(spec);
turn out the fix was in the manifest XML
<activity android:name=".MyTabActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DateActivity" android:label="@string/app_name"></activity>
<activity android:name=".RandomNumActivity" android:label="@string/app_name"></activity>
the last two activities were not present before, and it would fail. I added them (thanks to Ngetha's suggestion above!) and it worked perfectly. For the tutorial's example itself, it would be the three artists, albums, and songs activities that you would need to add
I guess I learned that every activity needs to be listed in the manifest, makes sense I just didn't think of it while following the example to the T.
Is it true that this is the case? all activities must be in the manifest?
I'm only starting programming for the Android, and I encountered this problem, too. I have to say I was quite frustrated with it. Doing what Ngetha suggested helped me out, but I also had to edit my code a little. What I noticed is that apparently the Android does not like sub-classed Activities. At all. I thought I would make my code cleaner by encapsulating everything, but that's apparently not okay. I had to move my classes to separate files. I hope this helps other new programmers with the same encapsulation problem I had.
Try this in your manifest:
<activity android:name=".AlbumsActivity" android:label="@string/app_name"></activity>
<activity android:name=".ArtistsActivity" android:label="@string/app_name"></activity>
<activity android:name=".SongsActivity" android:label="@string/app_name"></activity>
SOLVED I was getting nullPointerException after .addTab(spec) when returning back from a launched intent. I did not get the error on the initial entry to this activity.
I solved it by adding:
TabHost tabHost = (TabHost) getTabHost(); tabHost.setCurrentTab(0); // this stopped the nullPointerException ..... .... tabHost.addTab(spec);