Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBarSherlock setCustomView not working in Android 3.2

I recently upgraded my application to use ActionBarSherlock 4.1. Since the upgrade users running the app on Honeycomb experiance a force close due to a null pointer exception when setting a custom view on the actionbar.

I add a custom view containing two spinners to the actionbar this works on Android 4.0 & 4.1 but is not working on 3.2.

            bar.setCustomView(R.layout.custom_action_bar);// spinners
            actionBarViewHolder= new CustomActionBarViewHolder();
            actionBarViewHolder.categorySpinner = (Spinner) findViewById(R.id.actionbar_catergory);
            actionBarViewHolder.sortBySpinner = (Spinner) findViewById(R.id.actionbar_sortby);

On Android 3.2 the spinners can not be found yet on 4.0 and 4.1 they views are found and anything runs smoothly.

I have not tried the application on a 3.0 emulator but I image the problem persists.

Any ideas what could be the problem?

<LinearLayout android:id="@+id/linearLayout1"
    android:layout_width="wrap_content" android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:layout_weight="0.5">

    <TextView android:id="@+id/textView1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Category:" />

    <Spinner android:id="@+id/actionbar_catergory"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:entries="@array/actionbar_spinner_catergory"
        android:background="@drawable/spinner_background" />
</LinearLayout>


<LinearLayout android:id="@+id/linearLayout1"
    android:layout_width="wrap_content" android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:layout_weight="0.5">

    <TextView android:id="@+id/textView2" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Sort By:" />

    <Spinner android:id="@+id/actionbar_sortby"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:entries="@array/actionbar_spinner_sortby" android:background="@drawable/spinner_background" />

</LinearLayout>

like image 638
bencallis Avatar asked Jul 24 '12 22:07

bencallis


1 Answers

Try this, its working fine on all devices or you can check the demo code here

getSupportActionBar().setCustomView(R.layout.actionbar_top); // load your layout
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

Images

enter image description here

like image 139
Mukesh Y Avatar answered Oct 20 '22 11:10

Mukesh Y