我正在尝试使用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
如果有人遇到同样的情况,请帮助。
答案 0 :(得分:0)
我有这个问题。在我的情况下,没有任何事情崩溃。它只是看起来像一个微调器,而不是Android 4.0中的搜索栏,将在android 2.3中的设置中显示。 在MENU xml中添加“mine:actionViewClass =”android.support.v7.widget.SearchView“”为我解决了这个问题:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mine="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
android:icon="@drawable/action_search"
android:title="@string/search_title"
mine:showAsAction="ifRoom|collapseActionView"
mine:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
在您的情况下“我的”应该用“com.android.wit”更改,而不是默认的“android”命名空间
同时检查一下:Android Support Library ActionBar not Working in 2.3 Device