2015-03-02 118 views
1

我是新來的Android所以請詳細TabListener在Android應用程序無法正常工作

我建立這顯示2個簡單的標籤一個App解釋。 我的代碼是:

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的黑皮書,我不明白。

請幫助

編輯: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) 

請幫助

+0

它是不是在logcat的任何錯誤? – 2015-03-02 10:37:17

+0

空指針異常它說。但不指向行號。在你的問題後 – 2015-03-02 10:39:56

+0

安置自己的logcat的錯誤@AdhishThite你只想要選項卡或操作欄中的選項卡,請:) – 2015-03-02 10:44:26

回答

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是片段的標籤,你可以使用你的。

+0

仍然寫作... – Xcihnegn 2015-03-02 11:22:17

+0

在這一行給出一個錯誤。說'必需的Android.app.ActionBar,找到'android.support.v7.app。動作條」。當我嘗試修復進口,錯誤不斷增加。 – 2015-03-02 11:22:44

+0

非常感謝。我會檢查並標記爲答案。 – 2015-03-02 11:44:44

相關問題