我是新來的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)
請幫助
它是不是在logcat的任何錯誤? – 2015-03-02 10:37:17
空指針異常它說。但不指向行號。在你的問題後 – 2015-03-02 10:39:56
安置自己的logcat的錯誤@AdhishThite你只想要選項卡或操作欄中的選項卡,請:) – 2015-03-02 10:44:26