Android: getSupportActionBar () siempre devuelve null en la biblioteca ActionBarSherlock


Estoy tratando de usar la biblioteca ActionBarSherlock para proporcionar soporte de ActionBar compatible con versiones anteriores con pestañas en mi aplicación Android, así que descargué la última compilación, construí la demo y la ejecuté.

Si va a la Barra de acciones, seleccione la pestaña de navegación, se bloquea cada vez. Aquí está el seguimiento de la pila:

09-03 02:34:47.940: ERROR/AndroidRuntime(3078): FATAL EXCEPTION: main  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.actionbarsherlock.sample.demos/com.actionbarsherlock.sample.demos.app.ActionBarTabNavigation}: java.lang.NullPointerException  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.access$1500(ActivityThread.java:122)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.os.Handler.dispatchMessage(Handler.java:99)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.os.Looper.loop(Looper.java:132)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.main(ActivityThread.java:4025)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at java.lang.reflect.Method.invokeNative(Native Method)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at java.lang.reflect.Method.invoke(Method.java:491)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at dalvik.system.NativeStart.main(Native Method)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): Caused by: java.lang.NullPointerException  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at com.actionbarsherlock.sample.demos.app.ActionBarTabNavigation.onCreate(ActionBarTabNavigation.java:19)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)  
09-03 02:34:47.940: ERROR/AndroidRuntime(3078): ... 11 more  

No puedo seguir adelante con mi aplicación hasta que esto se arregle. Escribí un montón de código, configuré la barra de acción en mi aplicación, e intenté ejecutarla, y se bloquea con un NPE debido al valor de retorno nulo en la llamada getSupportActionBar().

El código relevante está en realidad en la demo de la biblioteca:

public class ActionBarTabNavigation extends FragmentActivity implements ActionBar.TabListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportFragmentManager()
            .beginTransaction()
            .add(android.R.id.content, FragmentStackSupport.CountingFragment.newInstance(0))
            .commit();

        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for (int i = 0; i < 3; i++) {
            ActionBar.Tab tab = getSupportActionBar().newTab();
            tab.setText("Tab " + i);
            tab.setTabListener(this);
            getSupportActionBar().addTab(tab);
        }
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        getSupportFragmentManager()
            .beginTransaction()
            .replace(android.R.id.content, FragmentStackSupport.CountingFragment.newInstance(tab.getPosition()))
            .commit();
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }
}
Author: Idolon, 2011-09-03

14 answers

Debe agregar el tema Sherlock a su aplicación

<application android:icon="@drawable/icon" android:label="@string/app_name"
        android:debuggable="false" android:theme="@style/Theme.Sherlock">
 81
Author: weakwire,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-14 13:33:09

Tuve el mismo problema en el Android ICS 4.0.4. Estaba usando requestWindowFeature(Window.FEATURE_NO_TITLE); en la FragmentActivity, pero esto ocultaba la barra de acción en los dispositivos ICS+ que causaban que getSupportActionBar() fuera null.

Simplemente eliminó el:
requestWindowFeature(Window.FEATURE_NO_TITLE);

Y funcionó como un encanto.

Espero que ayude a alguien.

 62
Author: Mario Santos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-30 16:44:53

Otra razón por la que esto sucederá en dispositivos Honeycomb+ es porque el atributo windowNoTitle está establecido en su estilo. Deshazte de eso, ya que ActionBarSherlock lo eliminará automáticamente en los dispositivos pre-Honeycomb.

 37
Author: Donn Felker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-30 16:43:55

Otra razón por la que puede obtener null de getSupportActionBar() es intentar llamarlo antes de setContentView(R.layout.main) o en su ejemplo agregar un fragmento.

I refactorizado oncreate y poner equivocadamente getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); después de super.onCreate(savedInstanceState);

 14
Author: scottyab,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-26 16:24:17

Aquí hay uno divertido: no establezca el tema en -

    android:theme="@style/Theme.NoActionbar"
 9
Author: JY2k,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-19 20:26:21

Otra razón por la que puede obtener null de getSupportActionBar() es cuando la actividad se usa en un TabHost en Honeycomb+.

 6
Author: aleb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-13 16:13:18

Dependiendo de cómo escriba el código. asegúrese de haber establecido la barra de herramientas primero antes de llamarla.

  mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
 5
Author: Alia Ramli Ramli,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-08 04:10:30

Acabo de cambiar en Manifiesto

android:theme="@style/AppTheme.NoActionBar"

A:

  android:theme="@style/AppTheme"

Y el error se fue

 2
Author: CodeToLife,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-08 09:31:04

Cada vez que configuramos customview usando la biblioteca sherlock. simplemente elimine esta característica requestWindowFeature (Window.FEATURE_NO_TITLE); así hacemos customview usando sherlock bar library..

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    getSupportActionBar().setCustomView(R.layout.header_sherlock_xmllayout);
    header_tvleft = (TextView) findViewById(R.id.header_tvleft);
    header_tvleft.setText("Back");
 1
Author: harikrishnan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-10 10:57:10

Tuve este problema después de (sillily) olvidar llamar al super.onCreate()

 1
Author: Navarr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-24 20:41:29

Se declara Tema.Sherlock o Theme.Sherlock.Luz como su actividad o tema de aplicación en el manifiesto o usando un tema personalizado que hereda de uno de estos dos

Ejemplo: -

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light" >

También u puede usar el tema oscuro: -

android:theme="@style/Theme.Sherlock"
 1
Author: Dhaval,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-21 06:57:52

Me encontré con esto después de agregar una biblioteca a mi proyecto. El remedio era buscar en la biblioteca y eliminar cualquier nombre de estilos "AppTheme" si está utilizando este mismo nombre de tema en su manifiesto. No había un conflicto en mi Galaxy S4, Jelly Bean, mientras que había un conflicto en mi Galaxy Tab.

 0
Author: Chris Sprague,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-20 17:10:20

Agregué android:theme="@android:style/Theme.Dialog" a mi actividad en el archivo de manifiesto de Android en un intento de convertirlo en una actividad de diálogo. Esto también eliminará la barra de acciones, por lo tanto, un puntero nulo. Retírelo o no llame getSupportActioBar

 0
Author: mugume david,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-13 22:13:35

Cometo este error porque agrego la barra de herramientas en el archivo xml de fragmento. El código para encontrar la barra de herramientas en fragment es este:getActivity ().findViewById (id...), pero mi barra de herramientas está en el archivo xml de fragmento, por lo que no se encontró ninguna barra de herramientas y ninguna barra de herramientas beeing setSipprotActionBar() y también nada get when getSupportActionBar(). Así que recuerda: No pongas la barra de herramientas en el archivo xml de tu fragmento.

 0
Author: nikerxu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-16 13:03:26