一周我无法确定问题是什么.code:
<menu 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" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/action_search" android:title="@string/action_search" android:icon="@drawable/ic_search_dark"
app:showAsAction="collapseActionView|ifRoom" app:actionViewClass="android.support.v7.widget.SearchView"/></menu>
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
startActivity(new Intent(this,SettingsActivity.class));
return true;
case R.id.action_search:
return true;
}
return super.onOptionsItemSelected(item);
}
public abstract class MainFragments extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return super.onCreateView(inflater, container, savedInstanceState);
}}
private Cursor getAllArtists(String search) {
ContentResolver contentResolver = ourContext.getContentResolver();
String[] projection = {MediaStore.Audio.Artists._ID,
MediaStore.Audio.Artists.ARTIST,
MediaStore.Audio.Artists.NUMBER_OF_TRACKS,
MediaStore.Audio.Artists.NUMBER_OF_ALBUMS};
if (search != null && search.length() > 0) {
search = "LOWER("
+ MediaStore.Audio.Albums.ARTIST + ") LIKE LOWER('" + search + "%') ";
}
return contentResolver.query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
projection,
search,
null,
MediaStore.Audio.Artists.ARTIST + " ASC");
}
public ArrayList<MetaInfo> getArtists(String search) {
ArrayList<MetaInfo> infos = new ArrayList<>();
Cursor c = getAllArtists(search);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
MetaInfo info = new MetaInfo();
info.setId(c.getString(c.getColumnIndex(MediaStore.Audio.Artists._ID)));
info.setName(c.getString(c.getColumnIndex(MediaStore.Audio.Artists.ARTIST)));
info.setNumber_of_songs(c.getString(c.getColumnIndex(MediaStore.Audio.Artists.NUMBER_OF_TRACKS)));
info.setNumber_of_album(c.getString(c.getColumnIndex(MediaStore.Audio.Artists.NUMBER_OF_ALBUMS)));
infos.add(info);
}
c.close();
return infos;
}
public boolean onQueryTextSubmit(String query) {
searchName(query);
return false;
}
public boolean onQueryTextChange(String newText) {
searchName(newText);
return false;
}
private void searchName(String s) {
infos = database.getArtists(s);
adapter.addAll(infos);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu,inflater);
searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);
}
public class ArtistListAdapter extends BaseAdapter {
public void addAll(ArrayList<MetaInfo> metaInfos) {
infos.clear();
this.infos = metaInfos;
notifyDataSetChanged();
}
}
答案 0 :(得分:0)
确保您已更改目标活动的清单。
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEARCH" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>