Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findViewById returns null when searching for elements in the actionbar on android 3.2

I am working on an app which uses a custom actionbar layout, which is referenced via android:actionBarStyle and android:customNavigationLayout in the themes.xml. There are some elements in the bar, like a reload and some info button. On android 4 devices those buttons can be found via findViewById (inside the activity), but on android 3.2 findViewById returns null. This throws a NullPointerException then.

The app is using ActionbarSherlock 4.2.0

Here is the themes.xml:

 <style name="Theme.SRF.Tablet" parent="style/Theme.Sherlock">
     <item name="android:windowContentOverlay">@drawable/header_shadow</item>
     <item name="android:actionBarStyle">@style/TTTActionBar</item> </style>

 <style name="TTTActionBar" parent="style/Widget.Sherlock.ActionBar">
     <item name="android:background">@drawable/bg_header</item>
     <item name="android:customNavigationLayout">@layout/actionbar_custom</item>
 </style>

Activity:

    @Override
    protected void onCreate(Bundle _savedInstanceState) {
        setContentView(...);
        // next line throws NullPointerException on android 3.2
        findViewById(R.id.actionbar_custom_bt_refresh).setOnClickListener(mClickListener);
        ....
    }
like image 687
stoefln Avatar asked Jan 30 '14 10:01

stoefln


2 Answers

I dont know if this would help you, but from here:

Try adding:

<item name="customNavigationLayout">@layout/custom_action</item>

to go along with:

<item name="android:customNavigationLayout">@layout/custom_action</item>
like image 106
Chintan Soni Avatar answered Oct 05 '22 04:10

Chintan Soni


The views aren't guaranteed to be ready yet in onCreate, you'll want to wait until onCreateView or even onActivityCreated to be safe. Usually onCreateView is okay but it's always a good idea to do a null check on the View just in case.

onCreate

Note that this can be called while the fragment's activity is still in the process of being created. As such, you can not rely on things like the activity's content view hierarchy being initialized at this point. If you want to do work once the activity itself is created, see onActivityCreated(Bundle).

onCreateView

Called to have the fragment instantiate its user interface view. This is optional, and non-graphical fragments can return null (which is the default implementation). This will be called between onCreate(Bundle) and onActivityCreated(Bundle).

If you return a View from here, you will later be called in onDestroyView() when the view is being released.

onActivityCreated

Called when the fragment's activity has been created and this fragment's view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. It is also useful for fragments that use setRetainInstance(boolean) to retain their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onViewStateRestored(Bundle).

like image 39
Jon Avatar answered Oct 05 '22 03:10

Jon