我想搜索RecyclerView,但在Fragment里面。 我有err:java.lang.NullPointerException:尝试在空对象引用上调用接口方法'android.view.View android.view.MenuItem.getActionView()' enter image description here
代码Activitymain.Java
package com.example.contact;
import android.support.design.widget.TabLayout;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.SearchView;
import android.widget.Toast;
import com.example.contact.adapter.RecyclerViewAdapter;
import com.example.contact.adapter.ViewPagerAdapter;
import com.example.contact.fragment.CallFragment;
import com.example.contact.fragment.ContactFragment;
import com.example.contact.models.Contact;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;
private Toolbar toolbar;
private ContactFragment contactFragment ;
// private List<Contact> lstContact = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) findViewById(R.id.tablayout_id);
viewPager = (ViewPager) findViewById(R.id.viewpager_id);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
//Set to toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false); //Remove Title of Toolbar
//Add Fragment here
contactFragment = new ContactFragment();
adapter.addFragment(new CallFragment(),"GẦN ĐÂY");
adapter.addFragment(contactFragment, "DANH BẠ");
viewPager.setAdapter(adapter);
//Custom tablayout
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabTextColors(getResources().getColor(R.color.colorHintTextLight), getResources().getColor(R.color.colorTextLight));
/*SearchView searchView = (SearchView)findViewById(R.id.search);
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});*/
}
public void msg(View view){
Toast.makeText(getApplicationContext(), "Click!", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_toolbar, menu);
MenuItem menuItem = menu.findItem(R.id.search);
// SearchView searchView = (SearchView) menuItem.getActionView();
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
String userInput = newText.toLowerCase();
List<Contact> listNew = new ArrayList<>();
String name;
for (int i = 0 ; i< contactFragment.getLstContact().size(); i++){
name = contactFragment.getLstContact().get(i).getName();
if(name.toLowerCase().contains(userInput)){
listNew.add(contactFragment.getLstContact().get(i));
}
}
return false;
}
}
Fragmentcontact.Java
package com.example.contact.fragment;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.contact.models.Contact;
import com.example.contact.R;
import com.example.contact.adapter.RecyclerViewAdapter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ContactFragment extends Fragment {
View v;
private RecyclerView recyclerView;
private List<Contact> lstContact;
public ContactFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.contact_fragment, container, false);
recyclerView = (RecyclerView)v.findViewById(R.id.contact_recyclerview);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), lstContact);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
return v;
}
public void loadContacList(){
//Accessing to contact list and get content
ContentResolver resolver = getActivity().getContentResolver();
Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
List<String> lstPhoneNumber = new ArrayList<>();
Cursor phoneCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
Log.i("MY INFO", id + " = " + name);
while (phoneCursor.moveToNext()){
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
lstPhoneNumber.add(phoneNumber);
// Log.i("MY INFO", phoneNumber);
}
lstContact.add(new Contact(name, name.substring(0, 1).toUpperCase(), lstPhoneNumber.get(0), R.drawable.contact_image));
}
Collections.sort(lstContact, new CustomComparaterLetterContact());
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lstContact = new ArrayList<>();
loadContacList();
}
//Class to Compare lstContact for letters
private class CustomComparaterLetterContact implements Comparator<Contact> {
@Override
public int compare(Contact o1, Contact o2) {
return o1.getName().compareToIgnoreCase(o2.getName());
}
}
public List<Contact> getLstContact(){
return lstContact;
}
}