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

时间:2016-03-06 06:10:33

标签: android nullpointerexception android-tabhost swipe gesture

我正在使用Tabhost开展项目,我必须通过检测"手势"来实现滑动功能。以下是我的代码

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

请给我一个解决方案。任何解决方案将不胜感激..提前致谢

2 个答案:

答案 0 :(得分:0)

可能是因为您使用的是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"

答案 1 :(得分:0)

除了&#34; TabActivity在API级别13&#34;中被弃用的事实。和#34;应用程序应该使用Fragments而不是TabActivity。&#34; - Android Documentation

就个人而言,我认为您不应该使用findViewById,因为有一种方法可以获得TabHost。

  

public TabHost getTabHost()

     

返回活动用于托管其标签的TabHost。

但是,如果您看到该链接的“类概述”部分,则会显示示例XML布局以及您需要使用的相应XML ID。

您也无法清楚您所使用的代码中的位置

host.setCurrentTab

因为如果问题中的代码host为空,那么host.setup()会抛出错误。