gpt4 book ai didi

android - 在 android 中使用 ActionBar Inside Fragment 类

转载 作者:行者123 更新时间:2023-11-29 17:50:22 24 4
gpt4 key购买 nike

我正在尝试在 fragment 中创建滑动标签。引用this的教程关联。我正在使用 ViewPager 它。 下面是我的代码:-

   public class A extends Fragment {


private ViewPager pager;
private TabsAdapter mTabsAdapter;

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);

}

@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{

// TODO Auto-generated method stub
View v = LayoutInflater.from(getActivity()).inflate(R.layout.one_fragment,
null);

pager = new ViewPager(getActivity());
pager.setId(R.id.pager);
getActivity().setContentView(pager);

final ActionBar bar = getActivity().getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

mTabsAdapter = new TabsAdapter(getActivity(), pager);
mTabsAdapter.addTab(bar.newTab().setText("Mixed"), MixedFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Income"), MixedFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Expense"), MixedFragment.class, null);


return v;
}


}

当我运行应用程序时,它在

给出了空指针异常
 final ActionBar bar = getActivity().getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

请任何人告诉我如何解决这个问题。或者给我一些示例代码以在 Fragment(不是 FragmentActivity)中创建滑动标签。提前致谢。

下面是我的 list 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hm.example"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light">
<activity
android:name="com.hm.example.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

下面是我的 logcat 显示的内容:

   FATAL EXCEPTION: main
java.lang.NullPointerException
at com.hm.A.onCreateView(A.java:49)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1514)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

getActionBar() 可以返回 null。

public ActionBar getActionBar ()

Added in API level 11 Retrieve a reference to this activity's ActionBar.

Returns The Activity's ActionBar, or null if it does not have one.

您可能正在为 Activity 使用非操作栏主题。

有一个操作栏主题

android:theme="@android:style/Theme.Holo.Light"

关于android - 在 android 中使用 ActionBar Inside Fragment 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23307522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com