我正在尝试使用 v7 Appcompact 实现 ActionBar 支持库。我用 ActionBarActivity 扩展了我的 Activity 类。
我@style/Theme.AppCompat
用作主题。
以下是我的主要课程:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
System.out.println("********onCreateOptionsMenu*************");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_activity_actions, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
System.out.println("********onOptionsItemSelected*************");
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(this, "Search clicked!!!!!", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
我的菜单 xml 文件是:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:com.android.wit="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
android:icon="@drawable/action_search"
android:title="@string/search_title"
com.android.wit:showAsAction="always" />
</menu>
当我运行我的应用程序时,使用上面的代码,没有显示带有菜单的操作栏,当我单击设备菜单按钮时,我得到以下运行时异常并且应用程序崩溃。
10-04 14:49:03.356: I/InputDispatcher(2936): Delivering key to current input target: action: 0x0
10-04 14:49:03.361: I/System.out(12059): ********onCreateOptionsMenu*************
10-04 14:49:03.361: D/AndroidRuntime(12059): Shutting down VM
10-04 14:49:03.361: W/dalvikvm(12059): threadid=1: thread exiting with uncaught exception (group=0x41ab7930)
10-04 14:49:03.366: E/AndroidRuntime(12059): FATAL EXCEPTION: main
10-04 14:49:03.366: E/AndroidRuntime(12059): java.lang.NullPointerException
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:274)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.support.v7.app.ActionBarActivityDelegate.getMenuInflater(ActionBarActivityDelegate.java:89)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.support.v7.app.ActionBarActivity.getMenuInflater(ActionBarActivity.java:71)
10-04 14:49:03.366: E/AndroidRuntime(12059): at com.android.wit.MainActivity.onCreateOptionsMenu(MainActivity.java:42)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.app.Activity.onCreatePanelMenu(Activity.java:2550)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:224)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:141)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:280)
10-04 14:49:03.366: E/AndroidRuntime(12059): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:464)
10-04 14:49:03.366: E/AndroidRuntime(12059): at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:876)
10-04 14:49:03.366: E/AndroidRuntime(12059): at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1570)
10-04 14:49:03.366: E/AndroidRuntime(12059): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2083)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:4196)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:4125)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3173)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.os.Looper.loop(Looper.java:137)
10-04 14:49:03.366: E/AndroidRuntime(12059): at android.app.ActivityThread.main(ActivityThread.java:5328)
10-04 14:49:03.366: E/AndroidRuntime(12059): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 14:49:03.366: E/AndroidRuntime(12059): at java.lang.reflect.Method.invoke(Method.java:511)
10-04 14:49:03.366: E/AndroidRuntime(12059): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-04 14:49:03.366: E/AndroidRuntime(12059): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-04 14:49:03.366: E/AndroidRuntime(12059): at dalvik.system.NativeStart.main(Native Method)
10-04 14:49:03.396: E/Watchdog(2936): !@Sync 121
10-04 14:49:03.426: W/ContextImpl(2936): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1337 com.android.server.am.ActivityStack.startPausingLocked:1342 com.android.server.am.ActivityStack.finishActivityLocked:5865 com.android.server.am.ActivityStack.finishActivityLocked:5779 com.android.server.am.ActivityManagerService.handleAppCrashLocked:9428
10-04 14:49:03.431: D/CrashAnrDetector(2936): processName: com.android.wit
10-04 14:49:03.431: D/CrashAnrDetector(2936): broadcastEvent : com.android.wit data_app_crash
如果有人遇到过同样的情况,请帮忙。