I am trying to inflate my own on Action Bar. But I am getting null pointer exception.
This is my action bar XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Choose"
android:textColor="#000000"
android:id="@+id/mytext"
android:textSize="18sp"
android:layout_weight="0.94" />
</LinearLayout>
This is how I am inflating it
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose);
ActionBar bar = getActionBar();
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
bar.setCustomView(R.layout.action_layout);
The logcat-
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ceapp/com.ceapp.ChooseActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5136)
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:819)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at ceat.vfconnect.com.ceatapp.ChooseActivity.onCreate(ChooseActivity.java:26)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
Line no 26 is
bar.setCustomView(R.layout.action_layout);
What is wrong with my code?