K213
K213

Reputation: 311

NullPointerException on ActionBar

Okay i have read so much about this Error now that I think I go crazy Below is my onCreate for my Activity

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

    i_name= (EditText)findViewById(R.id.input_event_name);
    i_loc = (EditText)findViewById(R.id.input_event_location);
    i_date = (EditText)findViewById(R.id.input_event_date);
    i_time = (EditText)findViewById(R.id.input_event_time);
    i_desc = (EditText)findViewById(R.id.input_event_description);

    final LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.action_bar_edit_mode, null);

   this.getActionBar().setDisplayShowCustomEnabled(true);
   this.getActionBar().setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

I got the code from here to get 2 actionbar buttons. I went through the whole thing on my laptop, and Iam now trying to get it work on my pc.

Now I read something about, "I dont have an actionbar in my activity" and I read something about "I dont have title set". It worked fine on my laptop. I tried setting the Feature_action bar. getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

I also tried to set another theme, but nothing helped.

minSdkVersion 11 targetSdkVersion 21

logcat

java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2277)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5147)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.test.test2.NewEvent.onCreate(NewEvent.java:92)

Anyone knows whats going on and has another Idea i can try out?

Upvotes: 2

Views: 508

Answers (1)

Simas
Simas

Reputation: 44118

Since you're using ActionBarActivity (from support library), you need to invoke the support method:

getSupportActionBar();

Edit-Reply to the comment:

In the same way you did:

getSupportActionBar().setCustomView(view, new ActionBar.LayoutParams(ViewGroup
        .LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

However make sure to import only the ActionBar from the support lib:

import android.support.v7.app.ActionBar;

Upvotes: 3

Related Questions