Problem with android launch HelloTabWidget example - NullPointerException in addTab ()

I tried the Example Tab Layout , and I also fixed some typos in the example (and added all the actions to the manifest). However, when I run it in the emulator, I get a NullPointerException in the first line, which says

tabHost.addTab (specifications);

So, my question, of course, is. What is wrong with the example that might raise this exception? I use Eclipse Galileo and install the target package as Android 1.5. So far, I have not had other problems with other examples on the Android site.

package com.example.hellotabwidget; import android.app.TabActivity; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.widget.TabHost; public class HelloTabWidget extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) throws RuntimeException { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); // Resource object to get Drawables TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Reusable TabSpec for each tab Intent intent; // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused) //final Context context = getApplicationContext(); intent = new Intent().setClass(this, ArtistsActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("artists").setIndicator("Artists", res.getDrawable(R.drawable.ic_tab_artists)) .setContent(intent); tabHost.addTab(spec); //******** NullPointerException after running this line // Do the same for the other tabs intent = new Intent().setClass(this, AlbumsActivity.class); spec = tabHost.newTabSpec("albums").setIndicator("Albums", res.getDrawable(R.drawable.ic_tab_artists)) .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, SongsActivity.class); spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.ic_tab_artists)) .setContent(intent); tabHost.addTab(spec); tabHost.setCurrentTabByTag("artists"); } } 

main.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" 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" /> </LinearLayout> </TabHost> 
+4
source share
12 answers

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> 
+9
source

I just want to say thanks to everyone who posted here. Solved my problem (same as everyone else here). It was very difficult to get this to work. They should have greatly simplified the example.

To summarize the necessary changes.

  • add 3 activity lines that Ngetha says.
  • Move all 3 activity classes to separate files. For example, I ended up using my SongsActivity.java file (with suggestions about the eclipse error)

     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 had to create a res \ drawable directory and put all the icons, plus I made 3 xml files for icons like "ic_tab_songs.xml"
+2
source

I had the same problem. I started textbooks yesterday, and that was the only one who had problems.

A null pointer is thrown on this line on the first call

 tabHost.addTab(spec); 

it turned out that the fix was in the XML manifest

 <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 actions have not been presented before, and this will fail. I added them (thanks to Ngetha's suggestion above!), And it worked great. For the example lesson you will need three artists, albums and songs that you will need to add

I assume that I found out that each action should be indicated in the manifest, it makes sense, I just did not think about it, following the example of T.

Is this true? should all actions be in the manifest?

+1
source

Hey, follow these steps, and I'm sure your problem will go away: -

Create class HelloTabWidget.java

 package com.pericent; //this is package name import android.app.TabActivity; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.util.Log; import android.widget.TabHost; public class HelloTabWidget extends TabActivity { private String TAG="HelloTabWidget"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); // Resource object to get Drawables TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Resusable TabSpec for each tab Intent intent; // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused) intent = new Intent().setClass(this,ArtistsActivity.class); Log.v(TAG,"---artist activity is called---"); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("artists").setIndicator("Artists",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent); tabHost.addTab(spec); // Do the same for the other tabs intent = new Intent().setClass(this,AlbumsActivity.class); Log.v(TAG,"---album activity is called---"); spec = tabHost.newTabSpec("albums").setIndicator("Albums",res.getDrawable(R.drawable.ic_tab_albums)).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, SongsActivity.class); Log.v(TAG,"---song activity is called---"); spec = tabHost.newTabSpec("songs").setIndicator("Songs",res.getDrawable(R.drawable.ic_tab_songs)).setContent(intent); tabHost.addTab(spec); } } 

Create your second action: ArtistActivity.java

 package com.pericent; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class ArtistsActivity extends Activity { private String TAG="ArtistsActivity"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview=new TextView(this); textview.setText("This is Artist Activity"); setContentView(textview); Log.v(TAG,"---in artist activity---"); } } 

Create your third action: AlbumsActivity.java

 package com.pericent; import android.R; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class AlbumsActivity extends Activity{ private String TAG="AlbumsActivity"; @Override public void onCreate(Bundle savedInstance) { super.onCreate(savedInstance); TextView textview_album=new TextView(this); textview_album.setText("This is album activity"); setContentView(textview_album); Log.v(TAG,"---in album activity---"); } } 

Create your fourth action: SongsActivity.java

 package com.pericent; import android.R; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class SongsActivity extends Activity{ private String TAG="SongsActivity"; @Override public void onCreate(Bundle savedInstance) { super.onCreate(savedInstance); TextView textview_song=new TextView(this); textview_song.setText("This is song activity"); setContentView(textview_song); Log.v(TAG,"---in songs activity---"); } } 

Make a folder in res / drawable 3 XML files are created in this folder: the code of these files is as follows: -

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- When selected, use grey --> <item android:drawable="@drawable/ic_tab_artists_grey" android:state_selected="true" /> <!-- When not selected, use white--> <item android:drawable="@drawable/ic_tab_artists_white" /> </selector> 

In the above XML code, we use two images, the following images that must be saved in the same folder (res / drawable).

enter image description hereenter image description here

This is main.xml :

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/upper"> <TabHost 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" android:padding="5dp"> <HorizontalScrollView android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:fillViewport="true" android:scrollbars="none"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </HorizontalScrollView> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" /> </LinearLayout> </TabHost> </LinearLayout> 

This is AdroidManifest :

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pericent" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloTabWidget" 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=".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> </application> </manifest> 

I think that all this information will help if you have any problems, then let me tell you everything. I am always happy to help anyone.

+1
source

You should post some more code so that we can make more assumptions. Especially make sure you create an instance of tabHost Somewhere.

0
source

Have you added your activity to AndroidManifest.xml? You must tell the program about the activity you want to show, otherwise you will just get an error.

For example, you can put the following xml code inside the <application> element:

  <activity android:name=".ArtistsActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> </intent-filter> </activity> 

Hope this helps.

-Surya Wijaya Madjid

0
source

This is what worked for me:

I changed this line to use the "artists" tabHost.setCurrentTabByTag ("artists");

And then added the "." before TabAndroid (the main name of the activity) and added the three actions that Ngeta suggested.

 </application> 
0
source

I am just starting to program for Android, and I also ran into this problem. I must say, I was very upset by this. Doing what Ngeta suggested helped me, but I also had to change the code a bit. I noticed that, apparently, Android does not like subclassed actions. At all. I thought I would make my code cleaner by encapsulating everything, but this is apparently not in order. I had to move my classes to separate files. I hope this helps other new programmers with the same encapsulation problem as mine.

0
source

I had the same wired error.

Try the following:

If you use eclipse, remove the error, change the code (so that the application will be recompiled), and then it should work fine.

0
source

Better if you are stuck with such errors, try Project> Clean to regenerate automatically generated files.

0
source

SOLVED I got a nullPointerException after .addTab (spec) when returning from a running intent. I did not receive an error during the initial recording of this action.

I solved this by adding:

TabHost tabHost = (TabHost) getTabHost (); tabHost.setCurrentTab (0); // this stopped the nullPointerException exception ..... .... tabHost.addTab (specification);

0
source

I am also having problems with the tutorial tab widget.

I was onyl able to solve this breakthrough by reading this SO, thansk message for answers to the guys.

Returning to the tutorial ... the Android development team can improve its documentation.

They do infact, they tell us to add XML activity to the manifest, they just don't indicate it very well .. here is a quote:

Please note that this does not use the layout file. Just create a TextView, give it some text and set it as content. Duplicate this for each of the three activities and add the appropriate tags to the Android manifest file.

0
source

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


All Articles