对于Android 2.3.3(或任何以下API 16),我的应用无需显示任何错误。
对于Android 4.1设备(API 16)及更高版本,我收到此错误(应用具有最低API级别10):
java.lang.NullPointerException E/AndroidRuntime(1066): at at.kopp.huettenapp.MasterActivity.onCreateOptionsMenu(MasterActivity.java:347)
manifest.xml
<activity
android:name="at.kopp.huettenapp.MasterActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
...`
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_2_action_search"
android:title="Suche"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
MasterActivity.java
public class MasterActivity extends ActionBarActivity implements HutList.Callbacks, OnQueryTextListener, OnCloseListener {
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_master_actions, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); // << line 347
// set id to save state
searchView.setId(R.id.action_search_widget);
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(this);
return super.onCreateOptionsMenu(menu);
}
来自LogCat的完整错误堆栈
02-21 17:28:58.703: W/SupportMenuInflater(1066): Cannot instantiate class: android.app.SearchView
02-21 17:28:58.703: W/SupportMenuInflater(1066): java.lang.ClassNotFoundException: Didn't find class "android.app.SearchView" on path: DexPathList[[zip file "/data/app/at.kopp.huettenapp-1.apk"],nativeLibraryDirectories=[/data/app-lib/at.kopp.huettenapp-1, /system/lib]]
02-21 17:28:58.703: W/SupportMenuInflater(1066): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at android.support.v7.internal.view.SupportMenuInflater$MenuState.newInstance(SupportMenuInflater.java:480)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at android.support.v7.internal.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:441)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at android.support.v7.internal.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:462)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at android.support.v7.internal.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at at.kopp.huettenapp.MasterActivity.onCreateOptionsMenu(MasterActivity.java:340)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at android.app.Activity.onCreatePanelMenu(Activity.java:2504)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
02-21 17:28:58.703: W/SupportMenuInflater(1066): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
...
02-21 17:28:58.943: E/AndroidRuntime(1066): FATAL EXCEPTION: main
02-21 17:28:58.943: E/AndroidRuntime(1066): java.lang.NullPointerException
02-21 17:28:58.943: E/AndroidRuntime(1066): at at.kopp.huettenapp.MasterActivity.onCreateOptionsMenu(MasterActivity.java:347)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.app.Activity.onCreatePanelMenu(Activity.java:2504)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
02-21 17:28:58.943: E/AndroidRuntime(1066): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:413)
02-21 17:28:58.943: E/AndroidRuntime(1066): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:775)
02-21 17:28:58.943: E/AndroidRuntime(1066): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:198)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.view.Choreographer.doFrame(Choreographer.java:531)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.os.Handler.handleCallback(Handler.java:730)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.os.Looper.loop(Looper.java:137)
02-21 17:28:58.943: E/AndroidRuntime(1066): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-21 17:28:58.943: E/AndroidRuntime(1066): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 17:28:58.943: E/AndroidRuntime(1066): at java.lang.reflect.Method.invoke(Method.java:525)
02-21 17:28:58.943: E/AndroidRuntime(1066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-21 17:28:58.943: E/AndroidRuntime(1066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-21 17:28:58.943: E/AndroidRuntime(1066): at dalvik.system.NativeStart.main(Native Method)