1

I have set appcompat file to the projectproperties. There is no error in errorlog and neither in problems. The logcat shows the error java.nullpointer.exception in getActionBar. When i comment the line, the application runs well. Suggestion required on how this problem can be resolved

ActionBar actionBar= getActionBar();

View cview= getLayoutInflater().inflate(R.layout.actionbar_menu, null);
actionBar.setCustomView(cview);

Build Gradle File consist of the following

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.bumpintomums"
        minSdkVersion 15
        targetSdkVersion 19
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

Error Logcat

FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bumpintomums/com.current.amnepal.bumpintomum.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
            at android.app.ActivityThread.access$600(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.current.amnepal.bumpintomum.MainActivity.onCreate(MainActivity.java:83)
            at android.app.Activity.performCreate(Activity.java:4470)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
            at android.app.ActivityThread.access$600(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
            at dalvik.system.NativeStart.main(Native Method)
5
  • 1
    I extended ActionBarActivity to use getSupportActionBar() while extended Activity for getActionBar().In both the cases i got null pointer exception Commented Dec 24, 2014 at 3:28
  • post logcat, or error Commented Dec 24, 2014 at 3:42
  • Paste ur imports as well. Commented Dec 24, 2014 at 3:59
  • Post stack trace. Also just try to run this activity without accessing actionbar or getting referece to it and check if it is showing. Commented Dec 24, 2014 at 5:47
  • 1
    I am still unable to run my project.If anyone can help Commented Dec 24, 2014 at 7:31

4 Answers 4

2

You may check whether the "Theme" applied in the Activity

If it is a theme without ActionBar, you will get null if you try to get action bar.

Sign up to request clarification or add additional context in comments.

1 Comment

Theme.AppCompat.Light, this is the parent theme i used for the customized theme.
1

if you use activity.getActionBar() will only return a null, not a exception. please give your code and printStackTrace()

A nullpointer.exception only occurs when you call the null's method,such as null.getActionBar()

actionbar guide for more

PS: you use getActionBar().hide(); since getActionBar() is null, so your code is null.hide(), the exception occurs. if you want to use/hide actionBar, read the guide above.

NullpointerException-workround:

if(getActionBar() != null)
    getActionBar().hide();

use theme in AndroidManifest.xml

<activity android:theme="@style/Theme.AppCompat.Light" ... >

extends ActionBarActivity

public class FoorActivity extends ActionBarActivity

fill your menu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}

6 Comments

getWindow().requestFeature(Window.FEATURE_ACTION_BAR); getActionBar().hide(); Logcat shows the error in second line and it says it is caused by java.nullpointer.exception
you use getActionBar().hide() while the getActionBar() == null
I did what you say but i am unable to run actionbar in any of my classes. if shows error java.lang.nullpointerexception
have you create your menu? @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu items for use in the action bar MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_activity_actions, menu); return super.onCreateOptionsMenu(menu); }
Here i am getting a appcompat error. Wen i try to use appcompat for versio 19 it shows error in values 21 files error. if i use version 21 it shows error in all getActionbar() methods giving nullpointerexception
|
0

From what i can get from your question is that you are looking to design custom Action Bar for your Application,right?

If yes then below code may help you out:

public class MainActivity extends Activity {

       @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ActionBar bar = getActionBar();
            bar.setDisplayShowTitleEnabled(false);
            bar.setDisplayShowCustomEnabled(true);
            bar.setCustomView(R.layout.row);
            bar.show();
    }
}

Please Note that enabling customenabled property on action bar is important for your Actionbar to implement custom view.

bar.setDisplayShowCustomEnabled(true)

And also no need to use to LayoutInflator you could directly feed action bar custom layout by using

actionbar.setCustomView(your layout file)

8 Comments

It doesnot allow me to use getActionbar(). It gives Java.lang.nullpointerexception. I am using api 21 fo this project.
Are you sure you are calling getActionbar() after setContentView() method has been called in onCreate()?
@ranjana i noticed that you are using Appcompat v7 library,right?In that case,you should call getSupportActionBar() ,extend ActionBarActivity and define style/Theme.AppCompat.*childs* in your manifest.
And if supporting Android 1.x/2.x devices is not that important for you, i would recommend you using native ActionBar available after Api level 11
Check the updated question. I am using the latest android version to compile. As you said I tried to call getSupportActionbar(), it also showed the same error and I am using native actionbar.
|
0

I had Theme set to NoActionBar in Android manifest file. Moreover, my gradle build version and com.android.support:appcompat-v7:27.+' were out of sync. See if you have this problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.