web-dev-qa-db-ja.com

クラスAndroid.support.v7.widget.SearchViewをインスタンス化できません

誰か助けてください!プロジェクトに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
22
jgnt32

問題は悪いv7サポートライブラリにありました。すべてを正常に更新した後。

2
jgnt32

おそらくリリースビルドに表示されたものと似ていますが、v21サポートライブラリに切り替えたときにデバッグビルドには表示されませんでした。難読化の問題であることが判明し、この行をproguard-rules.txtファイルに追加すると修正されました。

-keep class Android.support.v7.widget.SearchView { *; }
56
waynesford

Android StudioとGradleを使用してプロジェクトをビルドしたときにも同じ問題が発生しました。v7互換のSearchViewを追加したいと思います。

解決策は、gradleプラグインのバージョンを0.9.1などから0.9.0に変更することです。それは私が見つけた一時的な方法です。それはおそらくgradleプラグインのバグです。お役に立てば幸いです。

    dependencies {
        classpath "com.Android.tools.build:gradle:0.9.0"
    }
3
Daniel.Wu