Tm Shaun
Tm Shaun

Reputation: 9

Application continuously crashing, I've looked at every file and cannot seem to figure out what's going on

Main Activity

package com.wickham.android.splash;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    // Setup the ActionBar and the Spinner in the ActionBar
    getActionBar().setDisplayShowTitleEnabled(true);
    getActionBar().setSubtitle("Practical Android");
    getActionBar().setTitle("Splash");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.actions, menu);
    return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.exit) {
        finish();
        return(true);
    }
    return(super.onOptionsItemSelected(item));
}

Error Code

2021-02-24 23:52:19.708 9309-9309/com.murach.splash E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.murach.splash, PID: 9309
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.murach.splash/com.wickham.android.splash.Splash}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        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.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
        at com.wickham.android.splash.Splash.onCreate(Splash.java:47)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        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) 

Upvotes: 0

Views: 70

Answers (2)

iamdhavalparmar
iamdhavalparmar

Reputation: 1218

Use getSupportActionBar() instead of getActionBar() because getActionBar() is depricater and returning null and you're getting null pointer exception so maybe this will work.

and also use this

import android.support.v7.app.AppCompatActivity

Upvotes: 0

Alex Rmcf
Alex Rmcf

Reputation: 924

All you need is to extend your Activity from AppCompatActivity:

public class Main extends AppCompatActivity{
}

don't use Activity or ActionBarActivity. I suggest you to use androidx imports, for example:

import androidx.appcompat.app.AppCompatActivity;

It may need to add implementetion to your dependencies {}block inside build.gradle(app module) file:

implementation 'androidx.appcompat:appcompat:1.2.0'

And then use getSupportActionBar() instead of getActionBar().

Upvotes: 1

Related Questions