작업 표시 줄의 검색보기가 작동하지 않습니다.
저는 Java 및 Android 개발에 익숙하지 않습니다. .NET Framework를 포함하는 작업 표시 줄을 원하는 앱에서 작업하고 SearchView
있습니다. Google 예제를 보았지만 작동하지 않습니다. 나는 뭔가 잘못하고 있어야하고 앱 개발을 포기하려고합니다 .DI가 검색했지만 도움이되는 것을 찾지 못했습니다. 어쩌면 너희들이 나를 도울 수 있습니다 :)
문제 : 검색 뷰가 정상적으로 열리지 만 그 후에도 아무 일도 일어나지 않고 내 searchManager.getSearchableInfo (getComponentName ()) 가 null을 반환 한다는 것을 알았습니다 . 또한 내가 제공 한 내 힌트가 내 검색 상자에 표시되지 않아 앱이 내 searchable.xml을 찾을 수 없다고 믿게됩니까? 충분한 이야기 :)
MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/app_label">
</searchable>
Androidmanifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.searchapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</application>
</manifest>
SearchableActivity.java
package com.example.searchapp;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class SearchableActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
private void doMySearch(String query) {
Log.d("Event",query);
}
}
그리고 여기에 참조 된 프로젝트에 대한 링크가 있습니다. 누군가가 저를 도와 주면 영원히 감사 할 것입니다!
귀하의 문제는 AndroidManifest에 있습니다. 나는 문서를 따라가는 것이기 때문에 거의 정확하게 당신과 똑같습니다. 그러나 그것은 불분명하거나 잘못되었습니다.
API 데모 소스에서 "android.app.searchable"메타 데이터가 "results"활동으로 이동해야한다는 것을 발견했으며 기본 활동 (또는 SearchView를 배치 한 활동)에서 "로 다른 활동을 가리 킵니다. android.app.default_searchable "입니다.
내 테스트 프로젝트의 Manifest 인 다음 파일에서 확인할 수 있습니다.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
<activity
android:name=".SearchActivity"
android:label="@string/app_name" >
<!-- This intent-filter identifies this activity as "searchable" -->
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- This metadata entry provides further configuration details for searches -->
<!-- that are handled by this activity. -->
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
searchable.xml의 힌트와 레이블이 하드 코딩 된 문자열이 아니라 참조라는 것도 중요합니다.
문제가 해결되기를 바랍니다. 그것을 알아내는 데 하루 종일 걸렸습니다 :(
Android guides explicitly says:
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
Which is in your case NOT TRUE. Because you don't want to search in MainActivity but in SearchableActivity. That means you should use this ComponentName instead of getComponentName() method:
ComponentName cn = new ComponentName(this, SearchableActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
Also it seems that both the android:hint and android:label attributes MUST be references to strings in strings.xml. Being lazy and hardcoding the string values doesn't seem to work at all :)
i.e. Don't do:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="Search for something..."
android:label="My App">
</searchable>
But definitely do (as the OP correctly did):
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/app_label">
</searchable>
If you are using Android Studio,
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
part may not work.
write full package name like
<meta-data
android:name="android.app.default_searchable"
android:value="com.example.test.SearchActivity" />
be careful with "xml/searchable.xml". If you made HARD CODE with android:hint
and android:label
.It will not work. Haha Should be :
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/app_name"
android:label="@string/search_lb"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>
There's actually no need use the "missing part" as described in the answers above. This method(using a SearchView
) works exactly as the official docs say till Using the Search widget. Here a slight modification needs to made as follows:
Properties in a SearchableInfo
are used to display labels, hints, suggestions, create intents for launching search results screens and controlling other affordances such as a voice button(docs).
Thus, the SearchableInfo
object we pass to the SearchView.setSearchableInfo(SearchableInfo s)
must contain proper references to the target activity which should be displayed to show the search results. This is done by manually passing the ComponentName
of our target activity. One constructor for creating a ComponentName
is
public ComponentName (String pkg, String cls)
pkg
and cls
are package names and class names of the target activity to be launched for displaying search results. Note that:
pkg
must point to the root package name of your project and,
cls
must be a fully qualified name of the class(i.e. the entire thing)
e.g.:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
// Get the SearchView and set the searchable configuration
String pkg = "com.example.musicapp"
String cls = "com.example.musicapp.search.SearchActivity"
ComponentName mycomponent = new ComponentName(pkg,cls);
SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(mycomponent));
searchView.setIconifiedByDefault(false); //if using on actionbar
return true;
}
Depending on your requirements take a look at the other ComponentName
constructors.
Make sure your searchables.xml
file is in the correct location (i.e, in your res/xml/
folder) and that the same file does not contain any errors - otherwise you will find that (SearchManager)getSystemService(Context.SEARCH_SERVICE).getSearchableInfo(componentName)
will return null, breaking your search functionality.
(Also, ensure you set componentName
in the appropriate manner, because the example shown in the Android guide is for only when you are making searches and displaying searches in the same Activity.)
Double check if the corresponding entry(ie search_hint and app_label) is present in values/strings.xml
eg
<string name="app_label">FunApp</string>
<string name="search_hint">Caps Off</string>
참고URL : https://stackoverflow.com/questions/11699206/cannot-get-searchview-in-actionbar-to-work
'Program Tip' 카테고리의 다른 글
"Maven 프로젝트 업데이트"중에 내부 오류가 발생했습니다. (0) | 2020.11.12 |
---|---|
Django는 특정 순서로 id 배열에서 QuerySet을 가져옵니다. (0) | 2020.11.12 |
"치명적 :이 작업은 작업 트리에서 실행해야합니다."라는 메시지가 나타나는 이유는 무엇입니까? (0) | 2020.11.12 |
문자열에서 부분 문자열 추출 (0) | 2020.11.12 |
0.-5가 -5로 평가되는 이유는 무엇입니까? (0) | 2020.11.12 |