扩展类android.support.v7.widget.SearchView时出错。关于SO的类似问题并没有解决它

时间:2016-02-16 22:11:12

标签: android searchview android-search inflate-exception android-searchmanager

这是一个SSCCE。我正在使用23.1.1版本的appcompat_v7支持库。我在一个版本为23 Android SDK的Genymotion模拟器上运行它。

最后给出了logcat的确切错误。

activity_main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v7.widget.SearchView
        android:id="@+id/mainActivity_searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java:

public class MainActivity extends Activity {
    SearchView searchView;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        searchView = (SearchView) findViewById(R.id.mainActivity_searchView);
        textView = (TextView) findViewById(R.id.textView);

        searchView.setSearchableInfo(((SearchManager) getSystemService(SEARCH_SERVICE)).getSearchableInfo(getComponentName())); 

        searchView.setIconifiedByDefault(false); 

        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SEARCH)) {
            String searchQuery = intent.getStringExtra(SearchManager.QUERY);
            performSearch(searchQuery);
        }
    }

    private void performSearch(String searchQuery) {
        textView.setText(searchQuery);
    }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tests.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="23" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="tests.test.module_s.ui.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

        <meta-data android:name="android.app.default_Searchable"
            android:value=".MainActivity" />
    </application>

</manifest>

logcat的:

java.lang.RuntimeException: Unable to start activity ComponentInfo{tests.test/tests.test.module_s.ui.MainActivity}: android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class android.support.v7.widget.SearchView

0 个答案:

没有答案