我使用此Tool自定义了操作栏。
除了我在Actionbar中的Searchview之外,一切都运作良好。
每次我启动我的应用程序时,它都会因为空指针异常而崩溃。 在修改我的操作栏之前,我没有遇到此错误。
有人知道如何解决这个问题吗?
感谢所有答案
-----------------------------错误代码:--------------- --------
07-29 12:57:44.049: E/AndroidRuntime(25189): FATAL EXCEPTION: main
07-29 12:57:44.049: E/AndroidRuntime(25189): Process: com.skatepedia.android, PID: 25189
07-29 12:57:44.049: E/AndroidRuntime(25189): java.lang.NullPointerException
07-29 12:57:44.049: E/AndroidRuntime(25189): at com.skatepedia.android.LexikonActivity.onCreateOptionsMenu(LexikonActivity.java:163)
07-29 12:57:44.049: E/AndroidRuntime(25189): at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
07-29 12:57:44.049: E/AndroidRuntime(25189): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:489)
07-29 12:57:44.049: E/AndroidRuntime(25189): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:853)
07-29 12:57:44.049: E/AndroidRuntime(25189): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:273)
07-29 12:57:44.049: E/AndroidRuntime(25189): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-29 12:57:44.049: E/AndroidRuntime(25189): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-29 12:57:44.049: E/AndroidRuntime(25189): at android.view.Choreographer.doFrame(Choreographer.java:543)
07-29 12:57:44.049: E/AndroidRuntime(25189): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-29 12:57:44.049: E/AndroidRuntime(25189): at android.os.Handler.handleCallback(Handler.java:733)
07-29 12:57:44.049: E/AndroidRuntime(25189): at android.os.Handler.dispatchMessage(Handler.java:95)
07-29 12:57:44.049: E/AndroidRuntime(25189): at android.os.Looper.loop(Looper.java:136)
07-29 12:57:44.049: E/AndroidRuntime(25189): at android.app.ActivityThread.main(ActivityThread.java:5144)
07-29 12:57:44.049: E/AndroidRuntime(25189): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 12:57:44.049: E/AndroidRuntime(25189): at java.lang.reflect.Method.invoke(Method.java:515)
07-29 12:57:44.049: E/AndroidRuntime(25189): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-29 12:57:44.049: E/AndroidRuntime(25189): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
07-29 12:57:44.049: E/AndroidRuntime(25189): at dalvik.system.NativeStart.main(Native Method)
--------------------- Lexikon活动中的错误:------------------
getMenuInflater().inflate(R.menu.main, menu);
MenuItem menuItem = menu.findItem(R.id.search_Action);
SearchView sw = (SearchView) MenuItemCompat.getActionView(menuItem); // Null Pointer
----------------------- Styles.xml:-------------------- -----------
<style name="Theme.Example" parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarItemBackground">@drawable/selectable_background_example</item>
<item name="android:popupMenuStyle">@style/PopupMenu.Example</item>
<item name="android:dropDownListViewStyle">@style/DropDownListView.Example</item>
<item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Example</item>
<item name="android:actionDropDownStyle">@style/DropDownNav.Example</item>
<item name="android:actionBarStyle">@style/ActionBar.Solid.Example</item>
<item name="android:actionModeBackground">@drawable/cab_background_top_example</item>
<item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_example</item>
<item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Example</item>
<!-- Light.DarkActionBar specific -->
<item name="android:actionBarWidgetTheme">@style/Theme.Example.Widget</item>
</style>
<style name="ActionBar.Solid.Example" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/ab_solid_example</item>
<item name="android:backgroundStacked">@drawable/ab_stacked_solid_example</item>
<item name="android:backgroundSplit">@drawable/ab_bottom_solid_example</item>
<item name="android:progressBarStyle">@style/ProgressBar.Example</item>
</style>
<style name="ActionBar.Transparent.Example" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:background">@drawable/ab_transparent_example</item>
<item name="android:progressBarStyle">@style/ProgressBar.Example</item>
</style>
<style name="PopupMenu.Example" parent="@android:style/Widget.Holo.ListPopupWindow">
<item name="android:popupBackground">@drawable/menu_dropdown_panel_example</item>
</style>
<style name="DropDownListView.Example" parent="@android:style/Widget.Holo.ListView.DropDown">
<item name="android:listSelector">@drawable/selectable_background_example</item>
</style>
<style name="ActionBarTabStyle.Example" parent="@android:style/Widget.Holo.ActionBar.TabView">
<item name="android:background">@drawable/tab_indicator_ab_example</item>
</style>
<style name="DropDownNav.Example" parent="@android:style/Widget.Holo.Spinner">
<item name="android:background">@drawable/spinner_background_ab_example</item>
<item name="android:popupBackground">@drawable/menu_dropdown_panel_example</item>
<item name="android:dropDownSelector">@drawable/selectable_background_example</item>
</style>
<style name="ProgressBar.Example" parent="@android:style/Widget.Holo.ProgressBar.Horizontal">
<item name="android:progressDrawable">@drawable/progress_horizontal_example</item>
</style>
<style name="ActionButton.CloseMode.Example" parent="@android:style/Widget.Holo.ActionButton.CloseMode">
<item name="android:background">@drawable/btn_cab_done_example</item>
</style>
<!-- this style is only referenced in a Light.DarkActionBar based theme -->
<style name="Theme.Example.Widget" parent="@android:style/Theme.Holo">
<item name="android:popupMenuStyle">@style/PopupMenu.Example</item>
<item name="android:dropDownListViewStyle">@style/DropDownListView.Example</item>
</style>
答案 0 :(得分:0)
请按以下方式填写您的代码。
在menu.xml中,将actionViewClass的命名空间从android:actionViewClass更改为app:actionViewClass
如下所示
<item android:id="@+id/search_action"
app:showAsAction="always|collapseActionView"
android:icon="@drawable/abc_ic_search"
android:title="@string/action_bar_search"
**android:actionViewClass**="android.support.v7.widget.SearchView"
/>
到
<item android:id="@+id/search_action"
app:showAsAction="always|collapseActionView"
android:icon="@drawable/abc_ic_search"
android:title="@string/action_bar_search"
**app:actionViewClass**="android.support.v7.widget.SearchView"
/>