Bastaix
Bastaix

Reputation: 845

Android TabActivity NullPointerException

Problems with the TabActivity...

I get this Exception

    java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1651)
    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:656)
    at android.widget.TabHost.setCurrentTab(TabHost.java:326)
    at android.widget.TabHost.addTab(TabHost.java:216)
    at home.android.yahtzee.activities.GameActivity.onCreate(GameActivity.java:36)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:143)
    at android.app.ActivityThread.main(ActivityThread.java:4196)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)

with this code

public class GameActivity extends TabActivity {

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.tab_handler);

        Gson gson = new Gson();
        Player player = WhoIsPlayingDialog.playerSelected;
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();
        tabHost.setup();
        tabHost.setTag(this);
        TabHost.TabSpec spec = tabHost.newTabSpec(player.getName());
        Intent intent = new Intent(GameActivity.this, GamePlayerActivity.class);
        intent.putExtra("player", gson.toJson(player));
        spec.setContent(intent);
        spec.setIndicator(player.getName(), res.getDrawable(player.getDroid()));
        tabHost.addTab(spec); <============ Line 36.
    }
}

And I do not know why. Can anyone tell me?

Upvotes: 0

Views: 757

Answers (3)

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

Try to use it like:

...    
TabHost.TabSpec spec;
Intent intent; 

intent = new Intent().setClass(getApplicationContext(), GamePlayerActivity.class);
spec = tabHost.newTabSpec(player.getName()).setIndicator(player.getName(),res.getDrawable(player.getDroid())).setContent(intent); 
tabHost.addTab(spec);

and remove tabHost.setup();

Upvotes: 1

a.ch.
a.ch.

Reputation: 8380

First, you do not need to call tabHost.setup() when you have already called getTabHost().

Second, you likely get NullPointerException because either res.getDrawable(player.getDroid()) or player.getName() returns null.

Upvotes: 1

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

Error is at line GameActivity.java:36 , no line number here . check what is null in this line . at a broad level i think Player player = WhoIsPlayingDialog.playerSelected; might be null if u did not initialize static playerSelected

Upvotes: 0

Related Questions