2015-07-03 58 views
0

我目前試圖自定義佈局添加到我的行動吧,但保持運行到一個空指針異常,並不能找出辦法解決它。空指針異常當試圖訪問動作條

這裏是我的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()被返回null發生,我不能找出原因。我延長AppCompatActivity和運行min API 7.

在此先感謝。

回答

4

更換getActionBar()getSupportActionBar()和改變你的進口量相匹配。

+0

工程就像一個魅力。謝謝。 – sBourne