android.widget.TabHost.setCurrentTab(int) 上的空对象引用

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

我正在与 Tabhost 合作一个项目,我必须通过检测 "gesture" 来实现滑动功能。下面是我的代码

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

通过点击选项卡可以更改选项卡。但是当我想使用

更改选项卡时
host.setCurrentTab(tab position)

来自一个方法。它总是显示

"void android.widget.TabHost.setCurrentTab(int) 空对象引用"

虽然我已经初始化了 tabhost

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

详细错误如下。

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

请提供解决方案。任何解决方案将不胜感激。提前致谢

这可能是因为您正在使用 android.R 但也可能不是,请仅使用 R。 只需替换这一行

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

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

替换

<TabHost android:id="@+id/tabhost"

<TabHost android:id="@android:id/tabhost"

除了“TabActivity 在 API 级别 13 中被弃用”和“应用程序应使用片段而不是 TabActivity”这一事实之外。 - Android Documentation

就我个人而言,我认为您不应该使用 findViewById,因为已经有一种获取 TabHost 的方法。

public TabHost getTabHost ()

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

不过,如果您看到 link 的 Class 概述部分,它会向您展示一个示例 XML 布局和您需要使用的适当 XML id .

也不清楚您在代码中的哪个位置调用

host.setCurrentTab

因为如果 host 对于您问题中的代码为 null,那么 host.setup() 将引发错误。