TabListener无法在Android应用程序中运行

时间:2015-03-02 10:30:46

标签: java android listener android-tabs

我是Android新手,请详细解释

我正在构建一个显示2个简单标签的应用程序。 我的代码是:

ActionBarImpl.java

package com.adhish.tabs1;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;


import android.util.Log;

public class ActionBarImpl extends ActionBarActivity {

public static Context myContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_action_bar);
    myContext = getApplicationContext();

    try
    {
        ActionBar actionbar = getActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        Tab tab1 = actionbar.newTab().setText("Tab 1");
        Tab tab2 = actionbar.newTab().setText("Tab 2");

        Fragment fragment1 = new FirstFragment();
        Fragment fragment2 = new SecondFragment();

        tab1.setTabListener(new MyTabsListener(fragment1));
        tab2.setTabListener(new MyTabsListener(fragment2));

        actionbar.addTab(tab1);
        actionbar.addTab(tab2);
    }
    catch (Exception e)
    {
        Log.e("Error !", e.toString());
        Log.e("StackTrace..",e.getStackTrace().toString());
    }
}
}

MyTabsListener.java

@SuppressLint("NewApi")
public class MyTabsListener implements ActionBar.TabListener {

public Fragment fragment;

//Create fragment for Activity
public MyTabsListener(Fragment fragment)
{
    this.fragment = fragment;
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction transaction)
{
    Toast.makeText(ActionBarImpl.myContext, "You have clicked again !", Toast.LENGTH_SHORT).show();
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction transaction)
{
    transaction.replace(R.id.actionbar, fragment);
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction transaction)
{
    transaction.remove(fragment);
}
}

FirstFragment和SecondFragment.java

@SuppressLint("NewApi")
public class FirstFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_first, container, false);
}
}

片段包含要显示的TextView。 但每当我运行应用程序时,我根本看不到任何标签。

我不明白为什么,因为代码是根据Android Black Book。

请帮助

编辑:LogCat

03-02 16:16:12.111  25889-25889/com.adhish.tabs1 E/dalvikvm﹕ dvmPauseGc(AppLaunch) called - cookie=0x1cc7 (f=0x1)
03-02 16:16:12.121  25889-25889/com.adhish.tabs1 I/PersonaManager﹕ getPersonaService() name persona_policy
03-02 16:16:12.156  25889-25889/com.adhish.tabs1 D/skia﹕ GFXPNG PNG bitmap created width:48 height:48 bitmap id is 180
03-02 16:16:12.181  25889-25889/com.adhish.tabs1 I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
03-02 16:16:12.181  25889-25889/com.adhish.tabs1 W/dalvikvm﹕ VFY: unable to resolve virtual method 11352: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
03-02 16:16:12.181  25889-25889/com.adhish.tabs1 D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
03-02 16:16:12.181  25889-25889/com.adhish.tabs1 I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
03-02 16:16:12.181  25889-25889/com.adhish.tabs1 W/dalvikvm﹕ VFY: unable to resolve virtual method 11358: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
03-02 16:16:12.181  25889-25889/com.adhish.tabs1 D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
03-02 16:16:12.186  25889-25889/com.adhish.tabs1 I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
03-02 16:16:12.186  25889-25889/com.adhish.tabs1 W/dalvikvm﹕ VFY: unable to resolve virtual method 9046: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
03-02 16:16:12.186  25889-25889/com.adhish.tabs1 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
03-02 16:16:12.191  25889-25889/com.adhish.tabs1 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
03-02 16:16:12.191  25889-25889/com.adhish.tabs1 W/dalvikvm﹕ VFY: unable to resolve virtual method 372: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-02 16:16:12.191  25889-25889/com.adhish.tabs1 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-02 16:16:12.191  25889-25889/com.adhish.tabs1 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
03-02 16:16:12.191  25889-25889/com.adhish.tabs1 W/dalvikvm﹕ VFY: unable to resolve virtual method 394: Landroid/content/res/TypedArray;.getType (I)I
03-02 16:16:12.191  25889-25889/com.adhish.tabs1 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-02 16:16:12.191  25889-25889/com.adhish.tabs1 D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 181
03-02 16:16:12.196  25889-25889/com.adhish.tabs1 D/skia﹕ GFXPNG PNG bitmap created width:42 height:126 bitmap id is 182
03-02 16:16:12.201  25889-25889/com.adhish.tabs1 D/skia﹕ GFXPNG PNG bitmap created width:18 height:18 bitmap id is 183
03-02 16:16:12.201  25889-25889/com.adhish.tabs1 D/skia﹕ GFXPNG PNG bitmap created width:18 height:18 bitmap id is 184
03-02 16:16:12.206  25889-25889/com.adhish.tabs1 D/skia﹕ GFXPNG PNG bitmap created width:18 height:18 bitmap id is 185
03-02 16:16:12.211  25889-25889/com.adhish.tabs1 D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 186
03-02 16:16:12.216  25889-25889/com.adhish.tabs1 D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 187
03-02 16:16:12.226  25889-25889/com.adhish.tabs1 E/Error !﹕ java.lang.NullPointerException
03-02 16:16:12.226  25889-25889/com.adhish.tabs1 E/StackTrace..﹕ [Ljava.lang.StackTraceElement;@42820020
03-02 16:16:12.321  25889-25889/com.adhish.tabs1 I/﹕ PLATFORM VERSION : JB-MR-2
03-02 16:16:12.361  25889-25889/com.adhish.tabs1 I/HWUI﹕ EGLImpl-HWUI Protected EGL context created
03-02 16:16:12.466  25889-25889/com.adhish.tabs1 D/OpenGLRenderer﹕ Enabling debug mode 0
03-02 16:16:12.491  25889-25889/com.adhish.tabs1 D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 188
03-02 16:16:12.531  25889-25889/com.adhish.tabs1 E/dalvikvm﹕ dvmResumeGc(0x1cc7, 0) called (f=0x1)

请帮助

1 个答案:

答案 0 :(得分:1)

首先,您的活动会延长ActionBarActivity,因此要获得ActionBar

ActionBar actionbar = getSupportActionBar()

并在TabListener更好地使用代码,例如:

private ActionBarActivity mActivity;
private String mTag;

//constructor
public MyTabsListener(ActionBarActivity activity, String tag, Fragment fragment)
{
     this.fragment = fragment;
     mActivity = activity;
     mTag = tag;
}



public void onTabSelected(Tab tab, FragmentTransaction ft) { 
  // Check if the fragment is already initialized 
  FragmentTransaction mft = mActivity.getSupportFragmentManager().beginTransaction();
  Fragment mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
      if(mFragment != null)
      {
          mft.attach(fragment).commit();
      }
      else
      {
          mft.add(R.id.actionbar, fragment, mTag).commit();
      }
} 

public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
        FragmentTransaction mft = mActivity.getSupportFragmentManager().beginTransaction();
        mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
        if (mFragment != null) {
            mft.detach(mFragment).commit();
        }

此外,您需要更改活动MyTabsListener中的onCreate个致电代码:

tab1.setTabListener(new MyTabsListener(this, "Frist_Frag", fragment1));
tab2.setTabListener(new MyTabsListener(this, "Second_Frag", fragment2));

First_FragSecond_Frag是片段代码,您可以使用自己的代码。