2015-07-03 2 views
0

В настоящее время я пытаюсь добавить пользовательский макет в свою панель действий, но продолжаю работать в исключении нулевого указателя и не могу определить способ его устранения.Исключение Null Pointer при попытке получить доступ к ActionBar

Вот мой onCreateOptionsMenu

@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    ActionBar mActionBar = getActionBar(); 
    LayoutInflater mInflater = LayoutInflater.from(this); 

    View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null); 
    TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.menuText); 
    mTitleTextView.setText("Insert Title Here"); 

    ImageButton imageButton = (ImageButton) mCustomView.findViewById(R.id.menuPlusButton); 
    imageButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(), "Start New Course", Toast.LENGTH_LONG).show(); 
     } 
    }); 

    mActionBar.setDisplayShowHomeEnabled(false); 
    mActionBar.setDisplayShowTitleEnabled(false); 
    mActionBar.setCustomView(mCustomView); 
    mActionBar.setDisplayShowCustomEnabled(true); 
    return super.onCreateOptionsMenu(menu); 
} 

ошибка, кажется, происходит в mActionBar.setDisplayShowHomeEnabled (ложной), который указывает, что ActionBar mActionBar = getActionBar() возвращается нуль, и я не могу понять, почему. Я расширяю AppCompatActivity и использую min API 7.

Заранее спасибо.

ответ

4

Замените getActionBar() на getSupportActionBar() и измените ваш импорт для соответствия.

+0

Работает как очарование. Благодарю. – sBourne

Смежные вопросы