Problem running android HelloTabWidget example - NullPointerException on addTab()

后端 未结 12 1082
陌清茗
陌清茗 2021-01-13 17:17

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

相关标签:
12条回答
  • 2021-01-13 17:48

    You should post some more of the code so that we can make better assumptions. Especially make sure you instantiate the tabHost Somewhere.

    0 讨论(0)
  • 2021-01-13 17:53

    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.

    1. add the 3 activity lines Ngetha says.
    2. 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);
      }
      }
      
    3. I had to create a res\drawable directory and place all icons there plus I made 3 xml files for the icons such as "ic_tab_songs.xml"
    0 讨论(0)
  • 2021-01-13 17:53

    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?

    0 讨论(0)
  • 2021-01-13 17:55

    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.

    0 讨论(0)
  • 2021-01-13 17:57

    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> 
    
    0 讨论(0)
  • 2021-01-13 17:59

    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);

    0 讨论(0)
提交回复
热议问题