请有人帮助我!我尝试将appcompat搜索视图添加到我的项目中。不成功。当我将android.support.v7.widget.SearchView更改为SearchView(API> 10)时,一切都很好。
这是我的菜单:
<item android:id="@+id/action_search"
android:title="@string/search_view_title"
android:icon="@drawable/action_search"
sabd:showAsAction="always|collapseActionView"
sabd:actionViewClass="android.support.v7.widget.SearchView" />
这是我尝试使用SeachView的方法:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.teach, menu);
mSearchItem = menu.findItem(R.id.action_search);
mSearchItem.setVisible(true);
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
mSearchView.setQueryHint(getString(R.string.search_view_hint));
mSearchView.setOnQueryTextListener(this);
}
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.rgups.time"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Base.AppCompat.Light"
android:name="RTApplication">
<activity
android:name="ru.rgups.time.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".fragments.WelcomeActivity"></activity>
<service android:name=".spice.SampleSpiceService"></service>
<activity android:name=".activities.AuthActivity"></activity>
<activity
android:name=".activities.HomeWorkActivity"
android:uiOptions="splitActionBarWhenNarrow">
</activity>
<receiver android:name="ru.rgups.time.receiver.HomeWorkNotificationReceiver">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</receiver>
</application>
</manifest>
这就是我在日志中看到的:
03-22 21:46:58.583: W/SupportMenuInflater(28798): Cannot instantiate class: android.support.v7.widget.SearchView
03-22 21:46:58.583: W/SupportMenuInflater(28798): java.lang.reflect.InvocationTargetException
03-22 21:46:58.583: W/SupportMenuInflater(28798): at java.lang.reflect.Constructor.constructNative(Native Method)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v7.internal.view.SupportMenuInflater$MenuState.newInstance(SupportMenuInflater.java:482)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v7.internal.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:441)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v7.internal.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:462)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v7.internal.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at ru.rgups.time.fragments.TeachersListFragment.onCreateOptionsMenu(TeachersListFragment.java:109)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1582)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1956)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:225)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.view.Choreographer.doFrame(Choreographer.java:543)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.os.Handler.handleCallback(Handler.java:733)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.os.Handler.dispatchMessage(Handler.java:95)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.os.Looper.loop(Looper.java:136)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at java.lang.reflect.Method.invoke(Method.java:515)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at dalvik.system.NativeStart.main(Native Method)
03-22 21:46:58.583: W/SupportMenuInflater(28798): Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class <unknown>
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v7.widget.SearchView.<init>(SearchView.java:262)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.support.v7.widget.SearchView.<init>(SearchView.java:254)
03-22 21:46:58.583: W/SupportMenuInflater(28798): ... 31 more
03-22 21:46:58.583: W/SupportMenuInflater(28798): Caused by: java.lang.reflect.InvocationTargetException
03-22 21:46:58.583: W/SupportMenuInflater(28798): at java.lang.reflect.Constructor.constructNative(Native Method)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
03-22 21:46:58.583: W/SupportMenuInflater(28798): ... 39 more
03-22 21:46:58.583: W/SupportMenuInflater(28798): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010069 a=-1}
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.content.res.Resources.loadDrawable(Resources.java:2068)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.widget.ImageView.<init>(ImageView.java:129)
03-22 21:46:58.583: W/SupportMenuInflater(28798): at android.widget.ImageView.<init>(ImageView.java:119)
03-22 21:46:58.583: W/SupportMenuInflater(28798): ... 42 more
答案 0 :(得分:56)
我有一个类似的可能出现在我的发布版本上但不是我的调试版本切换到v21支持库时。原来是一个混淆问题,并将此行添加到我的proguard-rules.txt
文件修复它:
-keep class android.support.v7.widget.SearchView { *; }
答案 1 :(得分:3)
当我使用Android Studio和Gradle构建项目时,我遇到了同样的问题。我想添加一个v7 compat的SearchView。
解决方案是将gradle插件版本从0.9.1或其他版本更改为0.9.0。这是我发现的一种临时方式。它可能是gradle插件的错误。 希望有所帮助。
dependencies {
classpath "com.android.tools.build:gradle:0.9.0"
}
答案 2 :(得分:2)
问题出在糟糕的v7支持库中。更新后一切都很好。