Android 我的活动不';我看不出有什么搜索意图

Android 我的活动不';我看不出有什么搜索意图,android,xml,android-studio,searchview,Android,Xml,Android Studio,Searchview,我正在学习安卓系统为学院做一些项目。我正在做一个在web服务上查找数据的应用程序。但是我的活动没有抓住搜索意图(或者我认为是这样)。我遵循了Android开发者指南:。我遵循了它,但我的应用程序不起作用。我把我的代码放在这里: SearchActivity.java package com.example.javierortiz.pprog2_ac4; import android.app.SearchManager; import android.content.Intent; import

我正在学习安卓系统为学院做一些项目。我正在做一个在web服务上查找数据的应用程序。但是我的活动没有抓住搜索意图(或者我认为是这样)。我遵循了Android开发者指南:。我遵循了它,但我的应用程序不起作用。我把我的代码放在这里:

SearchActivity.java

package com.example.javierortiz.pprog2_ac4;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class SearchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())){
            String asdf = intent.getStringExtra(SearchManager.QUERY);
            Log.d("asdfasdfasdf", asdf);
        }
    }
}
searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:label= "@string/app_name"
    android:hint = "@string/search_hint">

</PreferenceScreen>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.javierortiz.pprog2_ac4">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SearchActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.SEARCH" />
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data  android:name="android.app.searchable"
                        android:resource="@xml/searchable" />
        </activity>
    </application>

</manifest>

activity_search.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SearchActivity">

    <SearchView
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>


提前感谢

如果要实现搜索视图侦听器,请尝试:

SearchView search = findViewById(R.id.search);
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

                @Override
                public boolean onQueryTextChange(String newText) {
                    return false;
                }

                @Override
                public boolean onQueryTextSubmit(String query) {
                    Log.d("onQueryTextChange", "query" + query);
                    // Do your task here

                    return false;
                }

            });

谢谢你,伙计。我更改了密码,但应用程序在手机上崩溃了。我不知道为什么会这样。应用程序被复制了,但当它在手机上启动时崩溃了。我删除了onCreate of my SearchActivity.java上的代码,并编写了您的。