我无法在ActionBar触发器中使我的SearchView成为我的SearchActivity接收的意图。我认为这个问题出现在Manifest / XML中,因为logcat甚至没有显示SearchActivity的onCreate方法中的日志。我已经使用these resources和其他人(包括SO回复)来尝试实施它,但没有运气。
清单:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activitiesFragments.MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
<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=".activtiesFragments.SearchActivity"/>
</activity>
<activity
android:name=".activitiesFragments.LoginActivity"
android:label="@string/login_button_text"
android:screenOrientation="portrait" />
<activity
android:name=".activitiesFragments.SignUpActivity"
android:label="@string/create_account_button"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activitiesFragments.LoginActivity" />
</activity>
<activity android:name=".activitiesFragments.SearchActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>
MainActivity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_messages, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (android.support.v7.widget.SearchView) menu.findItem(R.id.friends_menu_item).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
SearchActivity
package dev.workman.shoutout.activitiesFragments;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import dev.workman.shoutout.R;
public class SearchActivity extends AppCompatActivity {
ArrayList<String> query_results = new ArrayList<>();
ArrayAdapter<String> adapter;
ListView results_view;
String TAG = "SearchActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
results_view = (ListView) findViewById(R.id.results_list);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, query_results);
results_view.setAdapter(adapter);
Log.d(TAG, "onCreate: created searchview activity");
handle_intent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
query_results.clear();
handle_intent(getIntent());
}
public void handle_intent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.d(TAG, "handle_intent: query " + query);
// process the query, add values to the query_results adpater
// then call adapter.notifyDataSetChanged()
}
}
}
答案 0 :(得分:0)
我发现了我的问题。而不是XML /清单问题,我的问题是getComponentName()调用。
我把它改成了
new ComponentName(MainActivity.this, SearchActivity.class)
现在可以使用了。我希望这可以帮助其他人解决同样的问题