我正在尝试设置actionBar的图标并让它能够通过点击它this response说回来,问题是当我尝试打开它时应用程序总是关闭。我尝试在getActionBar().setIcon(R.drawable.logo_cartas);
和onCreate()
方法中包含onCreateOptionsMenu()
,但它总是失败。我还在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>
但它仍然失败,那么我在哪里放置getActionBar().setIcon(R.drawable.logo_cartas);
或我做错了什么?
onCreate()
方法:
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()
方法:
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输出:
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)
正如我所说,当我把它放在onCreateOptionsMenu()
菜单
答案 0 :(得分:1)
将getActionBar().setIcon(R.drawable.logo_cartas);
放入onCreate
从oncreateoptionmenu
中删除它答案 1 :(得分:0)
最重要的一行不是带图标的那一行。在onCreate中添加这些行以便能够返回。
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);