问题描述
我有一些麻烦,从我的片段设置副标题在我的工具栏。我不断收到一个空指针异常的setSubTitle。
I am having some trouble setting the sub-title in my Toolbar from my fragment. I keep getting a Null Pointer Exception at the setSubTitle.
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().getActionBar().setSubtitle("About"); // NULL POINTER EXCEPTION here
}
添加工具栏到主机的活动:
Adding the toolbar to the host activity:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Crashlytics.start(this);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null)
{
Log.w("Rakshak", "Toolbar is not null");
setSupportActionBar(toolbar);
}
我style.xml
My style.xml
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#1A7E99</item>
<item name="colorPrimaryDark">#16657A</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
下面是logcat的:
Here is the Logcat:
11-12 11:24:15.580: E/AndroidRuntime(22183): FATAL EXCEPTION: main
11-12 11:24:15.580: E/AndroidRuntime(22183): java.lang.NullPointerException
11-12 11:24:15.580: E/AndroidRuntime(22183): at com.driverdesignstudio.drvr.About.onActivityCreated(About.java:63)
11-12 11:24:15.580: E/AndroidRuntime(22183): at android.app.Fragment.performActivityCreated(Fragment.java:1703)
11-12 11:24:15.580: E/AndroidRuntime(22183): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
11-12 11:24:15.580: E/AndroidRuntime(22183): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
11-12 11:24:15.580: E/AndroidRuntime(22183): at android.app.BackStackRecord.run(BackStackRecord.java:694)
11-12 11:24:15.580: E/Androidenter code hereRuntime(22183): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
11-12 11:24:15.580: E/AndroidRuntime(22183): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
11-12 11:24:15.580: E/AndroidRuntime(22183): at android.os.Handler.handleCallback(Handler.java:800)
11-12 11:24:15.580: E/AndroidRuntime(22183): at android.os.Handler.dispatchMessage(Handler.java:100)
11-12 11:24:15.580: E/AndroidRuntime(22183): at android.os.Looper.loop(Looper.java:194)
11-12 11:24:15.580: E/AndroidRuntime(22183): at android.app.ActivityThread.main(ActivityThread.java:5371)
11-12 11:24:15.580: E/AndroidRuntime(22183): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 11:24:15.580: E/AndroidRuntime(22183): at java.lang.reflect.Method.invoke(Method.java:525)
11-12 11:24:15.580: E/AndroidRuntime(22183): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
11-12 11:24:15.580: E/AndroidRuntime(22183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-12 11:24:15.580: E/AndroidRuntime(22183): at dalvik.system.NativeStart.main(Native Method)
我一定要做出接口的听众,听片段开始和停止在我的片段活动设置字幕或有更简单的方法来从我的片段设置在我的工具栏字幕。
Do I have to make interface listeners and listen to the fragment start and stop in my Fragment Activity to set subtitles or is there an easier way to set subtitles in my toolbar from my fragments.
让我知道你需要再看我的code。
Let me know if you need to see any more of my code.
干杯。
推荐答案
要使用的工具栏和Appcompat 21,你必须使用一个AppCompatActivity用途:
To use the Toolbar and the Appcompat 21, you have to use an AppCompatActivity and use:
((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle("About");
这篇关于从片段设置字幕在我的工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!