【发布时间】:2015-03-02 10:30:45
【问题描述】:
我是 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)
请帮忙
【问题讨论】:
-
LogCat 有错误吗?
-
它说空指针异常。但不指向行号。
-
请在您的问题帖子中发布您的 LogCat 错误 :)
-
@AdhishThite 您只需要一个或多个带有操作栏的选项卡。
-
@ArunKumar 带有 ActionBar 的标签 请帮助
标签: java android listener android-tabs