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