0

I am trying to set Toolbar title where my code line is

View customView = getSupportActionBar().getCustomView(); TextView textViewToolbarTitle = (TextView) customView.findViewById(R.id.my_toolbar_title);

I am getting error as java.lang.NullPointerException at

View customView = getSupportActionBar().getCustomView();

Please help.

Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
Sakshi Gupta
  • 116
  • 11
  • Adding a null check on getSupportActionBar() is good thing ? – Sakshi Gupta Jun 06 '16 at 09:40
  • are you getting null pointer on `View customView` or `TextView textViewToolBarTitle` ? – Shubhank Jun 06 '16 at 09:48
  • i asked something else. can you post the full logcat in the question ? – Shubhank Jun 06 '16 at 09:55
  • MainActivity extends BaseActivity and in MainActivity, I have public boolean onCreateOptionsMenu(Menu menu) { boolean inflationStatus = super.onCreateOptionsMenu(menu); return inflationStatus; } and BaseActivity extends AppCompatActivity and I have public boolean onCreateOptionsMenu(Menu menu) { setToolbarTitle(); return true; } – Sakshi Gupta Jun 06 '16 at 10:55
  • private void setToolbarTitle() { View customView = getSupportActionBar().getCustomView(); TextView textViewToolbarTitle = (TextView) customView.findViewById(R.id.my_toolbar_title); } – Sakshi Gupta Jun 06 '16 at 10:55
  • Crash on this line View customView = getSupportActionBar().getCustomView(); – Sakshi Gupta Jun 06 '16 at 10:56
  • well, you don't have enough rep to chat. Most prob you are doing error by not extending `ActionBarAppCompatActivity`. Would you be interested in PM using email or discussing here only on comment ? – Shubhank Jun 06 '16 at 10:57
  • Sure. Please share your email id – Sakshi Gupta Jun 06 '16 at 11:00
  • @Shubhank, SakshiGupta: This is not how this site works. There is a chat, for extended messaging. – Phantômaxx Jun 06 '16 at 11:53
  • I know @BobMalooga. i have asked her few times to share logcat but due to no other option left to actually help her i had to help outside the network. – Shubhank Jun 06 '16 at 11:54
  • Well... if she's not collaborative, then don't help her. – Phantômaxx Jun 06 '16 at 11:57

1 Answers1

0

You will not be able to access custom view by that way which you have used in your code, for getting custom view use below lines of code in your activity, in onCreate() method after setContentView() line;

    LayoutInflater mInflater = LayoutInflater.from(this);

    View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);

try this and you will get your view to handle next task and events.

Vickyexpert
  • 3,147
  • 5
  • 21
  • 34