页面上的NullPointerException

时间:2018-07-01 13:49:59

标签: java android nullpointerexception

所以基本上我想在这里实现本教程:
https://github.com/medyo/android-about-page

我在gradle中添加了必要的依赖项。但是,当我打开AboutCreator活动时,我的应用程序崩溃了

AboutCreatorActivity的代码

public class AboutCreatorActivity extends AppCompatActivity {

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about_creator);
    //setTitle("About Page");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

    FancyAboutPage fancyAboutPage=findViewById(R.id.fancyaboutpage);
    //fancyAboutPage.setCoverTintColor(Color.BLUE); //Optional
    fancyAboutPage.setCover(R.drawable.coverimg);
    fancyAboutPage.setName("Shashank Singhal");
    fancyAboutPage.setDescription("Google Certified Associate Android Developer | Android App, Game, Web and Software Developer.");
    fancyAboutPage.setAppIcon(R.drawable.cakepop);
    fancyAboutPage.setAppName("Cake Pop Icon Pack");
    fancyAboutPage.setVersionNameAsAppSubTitle("1.2.3");
    fancyAboutPage.setAppDescription("Cake Pop Icon Pack is an icon pack which follows Google's Material Design language.\n\n" +
            "This icon pack uses the material design color palette given by google. Every icon is handcrafted with attention to the smallest details!\n\n"+
            "A fresh new take on Material Design iconography. Cake Pop offers unique, creative and vibrant icons. Spice up your phones home-screen by giving it a fresh and unique look with Polycon.");
    fancyAboutPage.addEmailLink("david_g@hotmail.co.uk");
    fancyAboutPage.addFacebookLink("https://www.facebook.com/david.g.161");
    fancyAboutPage.addLinkedinLink("https://www.linkedin.com/in/davidg1/");
    //fancyAboutPage.addGitHubLink("https://github.com/Shashank02051997");

}
}

错误:

  07-01 14:44:17.090 26813-26813/net.simplifiedlearning.firebaseauth E/AndroidRuntime: FATAL EXCEPTION: main
    Process: net.simplifiedlearning.firebaseauth, PID: 26813
    java.lang.RuntimeException: Unable to start activity ComponentInfo{net.simplifiedlearning.firebaseauth/net.simplifiedlearning.firebaseauth.AboutCreatorActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2822)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2897)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1598)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:251)
        at android.app.ActivityThread.main(ActivityThread.java:6572)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
        at net.simplifiedlearning.firebaseauth.AboutCreatorActivity.onCreate(AboutCreatorActivity.java:19)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2775)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2897) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1598) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:251) 
        at android.app.ActivityThread.main(ActivityThread.java:6572) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  

1 个答案:

答案 0 :(得分:1)

删除此行getSupportActionBar()。setDisplayHomeAsUpEnabled(true);