我是编程新手,我正在创建一个可以显示联系人的项目,但我在下一行收到错误,
rvContacts =(RecyclerView)rootview.findViewById(R.id.contact);
public class ContactsFragment extends Fragment {
private ContactsAdapter contactsAdapter;
private ArrayList<Contacts> contactList = new ArrayList<Contacts>();
private RecyclerView rvContacts;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview= inflater.inflate(R.layout.contacts,container,false);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()) {
@Override
protected int getExtraLayoutSpace(RecyclerView.State state) {
return 300;
}
};
rvContacts = (RecyclerView) rootview.findViewById(R.id.contact);
//rvContacts.setLayoutManager(LinearLayoutManager);
rvContacts.addOnItemTouchListener(
new RecyclerItemClickListener (getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
public void onItemClick(View view, int position) {
// TODO Handle item click
Intent call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
+ contactList.get(position).getContact()));
startActivity(call);
}
})
);
rvContacts.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
// FeedContextMenuManager.getInstance().onScrolled(recyclerView, dx, dy);
}
});
new AsyncHttpTask().execute();
return rootview;
}
public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {
@Override
protected void onPreExecute() {
// setProgressBarIndeterminateVisibility(true);
}
@Override
protected Integer doInBackground(String... params) {
Integer result = 0;
if (addStaticData().size() != 0) {
// contactsAdapter = new ContactsAdapter(getActivity(), contactList);
result = 1;
} else {
result = 0; //"Failed to fetch data!";
}
return result; //"Failed to fetch data!";
}
@Override
protected void onPostExecute(Integer result) {
// Download complete. Let us update UI
System.out.println("check 3");
if (result == 1) {
//contactsAdapter = new ContactsAdapter(getActivity(),contactList);
rvContacts.setAdapter(contactsAdapter);
} else {
Toast.makeText(getActivity(), "Failed to read data!", Toast.LENGTH_SHORT).show();
}
}
}
private ArrayList<Contacts> addStaticData() {
contactList.add(new Contacts(R.drawable.ic_person_black_36dp, "Human Resource", "+254755397332"));
contactList.add(new Contacts(R.drawable.ic_person_black_36dp, "Finance", "+254727438270"));
contactList.add(new Contacts(R.drawable.ic_person_black_36dp, "OA Sales", "+254727438270"));
contactList.add(new Contacts(R.drawable.ic_person_black_36dp, "Innovation", "+254755397356"));
contactList.add(new Contacts(R.drawable.ic_person_black_36dp, "Internal IT", "+254755397230"));
contactList.add(new Contacts(R.drawable.ic_person_black_36dp, "Help Desk", "+254755397231"));
return contactList;
}
private class ContactsAdapter extends RecyclerView.Adapter {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
}
}