11

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);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        Log.w("Rakshak", "Toolbar is not null");

        setSupportActionBar(toolbar);
    }
}

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>

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.

Let me know if you need to see any more of my code.

Cheers.

Ziem
  • 6,579
  • 8
  • 53
  • 86
DrkStr
  • 1,752
  • 5
  • 38
  • 90
  • can you show your code. – Rathan Kumar Nov 12 '14 at 06:22
  • Which bit do you want to see ? – DrkStr Nov 12 '14 at 06:23
  • 2
    where you called that particular line.. – Rathan Kumar Nov 12 '14 at 06:24
  • Added in the onActivityCreated method where I call that line. Not much to show. Let me know if you need to see any-more of the code. Cheers. – DrkStr Nov 12 '14 at 06:29
  • plz confirm you are **not use actionbar support library** ? – turtle Nov 12 '14 at 06:35
  • I have imported the AppCompact v-21 library into my project and I have the Material theme implemented. I have added the Toolbar to the activity that hosts this fragment. If you would like to see my implementation of the toolbar pls check my code in this question. http://stackoverflow.com/questions/26859339/trouble-implementing-material-theme – DrkStr Nov 12 '14 at 06:39
  • Try this answer from another thread with the same topic: http://stackoverflow.com/a/32220854/1665562 – VladimirVip Nov 27 '15 at 20:36

5 Answers5

51

To use the Toolbar and the Appcompat 21, you have to use an AppCompatActivity and use:

((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle("About");
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

instead of calling getActivity() get the activity reference from the onAttach() lifecycle method of Fragment.

if you are using sherlock actionbar then call getSherlock().getActionBar()

Rathan Kumar
  • 2,567
  • 2
  • 17
  • 24
0

You Can Create One Custom Layout For This And Then use in onCreateView method.

private ActionBar actionBar;

OnCreateView method:

actionBar = getActivity().getActionBar();
if (actionBar != null) {
    actionBar.setCustomView(R.layout.actionbar_inner_custom_view);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
Ziem
  • 6,579
  • 8
  • 53
  • 86
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
0

Declare a variable ActionBar toolbar and use it this way:

toolbar=((AppCompatActivity) getActivity()).getSupportActionBar();

toolbar.setHomeAsUpIndicator(R.drawable.ic_arrow_left);
toolbar.setTitle("Detalhes");
Guildencrantz
  • 1,875
  • 1
  • 16
  • 30
Sam
  • 51
  • 1
  • 4
0

If you are using Kotlin language for developmen you can do it this way:

(activity as AppCompatActivity).supportActionBar?.setSubtitle(R.string.your_string)
salmanwahed
  • 9,450
  • 7
  • 32
  • 55