Search code examples
androidnullpointerexceptionandroid-tabhostswipegesture

null object reference on android.widget.TabHost.setCurrentTab(int)


I am working a project with Tabhost where I have to implement the Swipe function by Detecting "gesture". Below is my code

public class GameItemListActivity extends TabActivity implements ViewPager.OnPageChangeListener {
public TabHost host;
private ViewPager pager;
GestureDetectorCompat gestureDetectorCompat;
private GestureDetector gestureScanner;
public int currentTabposition;
public TabHost.TabSpec spec;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    host = (TabHost) findViewById(android.R.id.tabhost);

   // pager = (ViewPager) findViewById(R.id.pager);
    Download_Class.applicationContext=GameItemListActivity.this;
    host.setup();

    spec = host.newTabSpec("tab1");
    spec.setContent(new Intent(this, Action_Activity.class));
    spec.setIndicator("Action");
    host.addTab(spec);

    spec = host.newTabSpec("tab2");
    spec.setContent(new Intent(this, Arcade_Activity.class));
    spec.setIndicator("Arcade");
    host.addTab(spec);

    spec = host.newTabSpec("tab3");
    spec.setContent(new Intent(this, Sport_Activity.class));
    spec.setIndicator("Sports");
    host.addTab(spec);

    spec = host.newTabSpec("tab4");
    spec.setContent(new Intent(this, Cards_Activity.class));
    spec.setIndicator("Top Games");
    host.addTab(spec);

    spec = host.newTabSpec("tab5");
    spec.setContent(new Intent(this, Racing_Activity.class));
    spec.setIndicator("Racing");
    host.addTab(spec);

    spec = host.newTabSpec("tab6");
    spec.setContent(new Intent(this, GameOfTheWeek.class));
    spec.setIndicator("Game Of\nThe Week");
    host.addTab(spec);

    spec = host.newTabSpec("tab7");
    spec.setContent(new Intent(this, Puzzle_Activity.class));
    spec.setIndicator("Puzzle");
    host.addTab(spec);
    currentTabposition= getTabHost().getCurrentTab();
    Log.d("chagecurrenttab", String.valueOf(getTabHost().getCurrentTab()));}

Tab changing is ok by Tapping the tab. But When i want to change the tab by using

host.setCurrentTab(tab position)

from a method. it always shows

"void android.widget.TabHost.setCurrentTab(int) on a null object reference"

Though I have init the tabhost

host = (TabHost) findViewById(android.R.id.tabhost);

The detail errors is given below.

03-06 11:53:07.717 19772-19772/com.javatechig.gridviewexample W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabHost.setCurrentTab(int)' on a null object reference
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at com.javatechig.gridviewexample.Picture.GameRelated.GameItemListActivity.setTabContent(GameItemListActivity.java:159)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at com.javatechig.gridviewexample.Picture.GameRelated.Arcade_Activity$2.onSwipeRight(Arcade_Activity.java:118)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at com.javatechig.gridviewexample.Picture.SwipeListener$GestureListener.onFling(SwipeListener.java:46)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.GestureDetector.onTouchEvent(GestureDetector.java:610)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at com.javatechig.gridviewexample.Picture.SwipeListener.onTouch(SwipeListener.java:24)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.View.dispatchTouchEvent(View.java:8832)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2569)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2610)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1779)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.app.Activity.dispatchTouchEvent(Activity.java:2853)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2571)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
03-06 11:53:07.727 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
03-06 11:53:07.737 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
03-06 11:53:07.737 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
03-06 11:53:07.737 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
03-06 11:53:07.737 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
03-06 11:53:07.737 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
03-06 11:53:07.737 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
03-06 11:53:07.737 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
03-06 11:53:07.737 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
03-06 11:53:07.737 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
03-06 11:53:07.737 19772-19772/com.javatechig.gridviewexample W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
03-06 11:53:07.737 19772-19772/com.javatechig.gridviewexample W/System.err:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2610)

Please provide me a solution. Any solution will be appreciate.. Thanks in advance


Solution

  • Other than the fact that "TabActivity was deprecated in API level 13" and "applications should use Fragments instead of TabActivity." - Android Documentation

    Personally, I don't think you should be using findViewById because there is a method to get the TabHost already.

    public TabHost getTabHost ()

    Returns the TabHost the activity is using to host its tabs.

    If you see the Class Overview section of that link, though, it shows you an example XML layout and the appropriate XML ids you need to use.

    It also isn't clear where in your code you are calling

    host.setCurrentTab
    

    Because if host were null for the code in your question, then host.setup() would be throwing an error.