0

I'm trying to set the icon of the actionBar and make it able to go back by tapping it as this response says, the problem is that the app always get closed when I try to open it. I've tried to include getActionBar().setIcon(R.drawable.logo_cartas); inside of onCreate() and onCreateOptionsMenu() methods, but it always fails. I've also included the icon on the manifest.xml

<application
        android:allowBackup="true"
        android:icon="@drawable/logo_cartas"
        android:logo="@drawable/logo_cartas"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
</application>

But it still fails, so where do I have to place getActionBar().setIcon(R.drawable.logo_cartas); or what am I doing wrong?

onCreate() method:

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

    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setIcon(R.drawable.logo_cartas);

}

onCreateOptionsMenu() method:

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_seleccion_tamanio, menu);
    return true;
}

logcat output:

02-18 14:53:08.202    1957-1957/com.example.nacho.cartas E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.example.nacho.cartas.MainActivity.onCreateOptionsMenu(MainActivity.java:27)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2490)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
        at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
        at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
        at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:979)
        at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
        at android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:79)
        at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:118)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5039)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

And as I said, the same thing happens when I place it in onCreateOptionsMenu() menu

Community
  • 1
  • 1
Drumnbass
  • 867
  • 1
  • 14
  • 35

2 Answers2

1

place getActionBar().setIcon(R.drawable.logo_cartas); in onCreate

remove it from oncreateoptionmenu

dvs
  • 644
  • 5
  • 14
  • As I said, if I place it in onCreate() method, Android Studio throws me a NullPinterException and the app fails when it treat to open. And the same thing happens when I place it in onCreateOptionsMenu() method. – Drumnbass Feb 18 '15 at 14:55
  • check answer again...what is your minimum api ? – dvs Feb 18 '15 at 18:23
0

The most important line is not the one with the icon. Add this lines in onCreate to be able to go back.

getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
ByteHamster
  • 4,884
  • 9
  • 38
  • 53