在主要活动的操作栏上实现searchview时,我遇到了一个问题。主要活动的底部导航菜单包含三个片段...
并且搜索视图应适用于每个片段...
公共类MainActivity扩展了AppCompatActivity实现的SearchView.OnQueryTextListener {
php artisan make:event MessageCreated
php artisan make:listener AddMessageId
MenuItem menuItem = menu.findItem(R.id.search); SearchView searchView =(SearchView)menuItem.getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
private Boolean LoadContact = false ;
private BottomNavigationView navigationView ;
private String SearchText;
int page;
AllContactFrag allContactFrag = new AllContactFrag();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("Special Contact");
navigationView =(BottomNavigationView) findViewById(R.id.navigationView);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame, new SpecialContactFrag());
ft.commit();
// navigationView.setSelectedItemId();
navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id)
{
case R.id.action_MyNotes:
Bundle bundle = new Bundle();
bundle.putString("text",SearchText);
Fragment AllNotes = new NotesFrag();
AllNotes.setArguments(bundle);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frame,AllNotes);
transaction.addToBackStack(null);
transaction.commit();
toolbar.setTitle("My Note");
return true;
case R.id.action_AllContact:
Bundle bundle2 = new Bundle();
bundle2.putString("text",SearchText);
Fragment AlxlContact = new AllContactFrag();
AlxlContact.setArguments(bundle2);
FragmentManager xmanager = getSupportFragmentManager();
FragmentTransaction xtransaction = xmanager.beginTransaction();
xtransaction.replace(R.id.frame,AlxlContact);
xtransaction.addToBackStack(null);
xtransaction.commit();
toolbar.setTitle("Phone Contact");
return true;
case R.id.action_Spec:
Fragment AlxxlContact = new SpecialContactFrag();
FragmentManager xxmanager = getSupportFragmentManager();
FragmentTransaction xxtransaction = xxmanager.beginTransaction();
xxtransaction.replace(R.id.frame,AlxxlContact);
xxtransaction.addToBackStack(null);
xxtransaction.commit();
toolbar.setTitle("Special Contact");
return true;
}
return true;
}
});
}
@Override
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);
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
}
//
searchView.setOnQueryTextListener(this);
return true;
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onQueryTextSubmit(String query) {
int id = navigationView.getSelectedItemId();
switch (id)
{
case R.id.action_MyNotes:
Fragment NoteFrgment = new NotesFrag();
((NotesFrag) NoteFrgment).onQueryTextSubmit(query);
case R.id.action_AllContact:
Fragment AllCont = new AllContactFrag();
((AllContactFrag) AllCont).onQueryTextSubmit(query);
return true;
case R.id.action_Spec :
Fragment Spec = new SpecialContactFrag();
((SpecialContactFrag) Spec).onQueryTextSubmit(query);
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
// Toast.makeText(getBaseContext(),newText,Toast.LENGTH_LONG).show();
SearchText = newText ;
int id = navigationView.getSelectedItemId();
switch (id)
{
case R.id.action_MyNotes:
Fragment NoteFrgment = new NotesFrag();
((NotesFrag) NoteFrgment).onQueryTextChange(newText);
case R.id.action_AllContact:
AllContactFrag AllCont = (AllContactFrag)getSupportFragmentManager().findFragmentById(R.id.frame);
((AllContactFrag) AllCont).onQueryTextChange(newText);
return true;
case R.id.action_Spec :
Fragment Spec = new SpecialContactFrag();
((SpecialContactFrag) Spec).onQueryTextChange(newText);
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
allContactFrag.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
/* public class SectionsPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragmentList = new ArrayList<>();
List<String> fragmentListTitle = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if(LoadContact){
return fragmentList.get(position);
}else {
return fragmentList.get(position);
}
}
@Override
public int getCount() {
return fragmentList.size();
}
public void AddFragment(Fragment fragment, String fragmentTitle)
{
fragmentList.add(fragment);
fragmentListTitle.add(fragmentTitle);
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentListTitle.get(position);
}
}
*/
//在片段
@Override
public boolean onQueryTextChange(String newText) {
// Toast.makeText(getBaseContext(),newText,Toast.LENGTH_LONG).show();
SearchText = newText ;
int id = navigationView.getSelectedItemId();
switch (id)
{
case R.id.action_MyNotes:
Fragment NoteFrgment = new NotesFrag();
((NotesFrag) NoteFrgment).onQueryTextChange(newText);
case R.id.action_AllContact:
AllContactFrag AllCont = (AllContactFrag)getSupportFragmentManager().findFragmentById(R.id.frame);
((AllContactFrag) AllCont).onQueryTextChange(newText);
return true;
case R.id.action_Spec :
Fragment Spec = new SpecialContactFrag();
((SpecialContactFrag) Spec).onQueryTextChange(newText);
}
return true;
}
在onQueryTextSubmit的片段中,上下文值变为null,因此应用程序崩溃,因为我想从SQLite读取数据,并且context为null