简体   繁体   English

具有AppCompat和ActionBar的NullPointerExcetption

[英]NullPointerExcetption with AppCompat and ActionBar

The Activity I have crashes everytime with this error. 我的活动每次都会因此错误而崩溃。 If I add any code related to getSupportActionBar() it crashes within that. 如果添加与getSupportActionBar()相关的任何代码,它将在其中崩溃。 Otherwise it crashes out of that 否则它会崩溃

public class DishTag extends ActionBarActivity  implements OnClickListener{
 //....
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       setContentView(R.layout.dish_tag);
       super.onCreate(savedInstanceState);
       context=this;
       //literally all the code I have
       ActionBar a=getSupportActionBar();
       a.setTitle("SD"); 
    }
}

To be noted and I don't know if this makes any difference but I am using a ViewPager on the page. 需要注意的是,我不知道这是否有什么区别,但是我在页面上使用ViewPager

The error is 错误是

11-30 22:06:38.212: E/AndroidRuntime(27315): Caused by:java.lang.NullPointerException
Attempt  to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a
null object reference

So I found the answer. 所以我找到了答案。

I tried setting the content view before runnning the onCreate 我尝试在运行onCreate之前设置内容视图

protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.dish_tag);
        super.onCreate(savedInstanceState);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM