我正在尝试使用FirebaseUI来简化我的recyclerview。我已阅读文档并提供以下代码:
POJO:
public class Polls {
private String Question;
private String user;
private String date;
private String voteCounter;
private String commentCounter;
private String Image_URL;
private String pollID;
public Polls(String Question, String Image_URL){
this.Question = Question;
this.Image_URL = Image_URL;
}
public String getPollID(){
return pollID;
}
public String getQuestion() {
return Question;
}
public String getUser() {
return user;
}
public String getDate() {
return date;
}
public String getVoteCounter() {
return voteCounter;
}
public String getCommentCounter() {
return commentCounter;
}
public String getPollImage() {
return Image_URL;
}
public void setQuestion(String question) {
this.Question = question;
}
public void setUser(String user) {
this.user = user;
}
public void setDate(String date) {
this.date = date;
}
public void setVoteCounter(String voteCounter) {
this.voteCounter = voteCounter;
}
public void setCommentCounter(String commentCounter) {
this.commentCounter = commentCounter;
}
public void setPollImage(String pollImage) {
this.Image_URL = pollImage;
}
}
这是我写给Firebase的地方,这是我读到的单独活动:
mSubmitPollCreation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
final String epochTime = String.valueOf(c.getTimeInMillis());
mEpochRef = mBaseRef.child("Polls").child(epochTime);
if (resultImageURL != null){
Map<String, Object> imageURL = new HashMap<String, Object>();
imageURL.put("Image_URL", resultImageURL);
mEpochRef.updateChildren(imageURL);
} else {
Toast.makeText(getApplicationContext(),getResources().getString(R.string.no_image_selected),Toast.LENGTH_LONG).show();
return;
}
//Add Question
Map<String, Object> pollQuestion = new HashMap<String, Object>();
pollQuestion.put("Question", mCreatePollQuestion.getText().toString());
Log.v("POLL_QUESTION_CREATED", mCreatePollQuestion.getText().toString());
mEpochRef.child("Polls").updateChildren(pollQuestion);
//Add Answer Counter defaulted to 0
// Map<String, Object> pollAnswerCounter = new HashMap<String, Object>();
// pollAnswerCounter.put("Vote_Count", 0);
// mEpochRef.updateChildren(pollAnswerCounter);
if (mNumberOfPollAnswersCreatedByUser > 5){
Toast.makeText(getApplicationContext(),getResources().getText(R.string.poll_answers_greater_than_five),Toast.LENGTH_LONG).show();
mNumberOfPollAnswersCreatedByUser = 5;
}
//add answers to Firebase
// for (int i = 0; i < mNumberOfPollAnswersCreatedByUser; i++){
// EditText editText = (EditText) mEditTextAnswerLayout.findViewWithTag(getResources().getString(R.string.cre ated_answer_editText_id)+String.valueOf(i+1));
// String editTextInputForAnswer =
String.valueOf(editText.getText());
// final String answerLabel = "Answer";
// Map<String, Object> answerChoice = new HashMap<String, Object>();
// answerChoice.put(answerLabel, editTextInputForAnswer);
// mEpochRef.child("Answers").child(String.valueOf(i+1)).updateChildren(pollAnswerCounter);
// mEpochRef.child("Answers").child(String.valueOf(i+1)).updateChildren(answer Choice);
// Toast.makeText(getApplicationContext(),editTextInputForAnswer,Toast.LENGTH_ SHORT).show();
// }
Intent toHomeActivity = new Intent(CreateActivity.this, HomeActivity.class);
toHomeActivity.putExtra("viewpager_position", 2);
startActivity(toHomeActivity);
}
});
以下是我从Firebase读取的内容,并尝试使用FirebaseUI RecyclerViewAdapter加载RecyclerView:
public class LiveFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private RecyclerView mRecyclerview;
private DatabaseReference mBaseRef;
private DatabaseReference mPollsRef;
private LinearLayoutManager mLayoutManager;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public LiveFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment LiveFragment.
*/
// TODO: Rename and change types and number of parameters
public static LiveFragment newInstance(String param1, String param2) {
LiveFragment fragment = new LiveFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
mBaseRef = FirebaseDatabase.getInstance().getReference();
mPollsRef = mBaseRef.child("Polls");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View v = inflater.inflate(R.layout.fragment_new, container, false);
mRecyclerview = (RecyclerView) v.findViewById(R.id.new_RecyclerView);
if (mRecyclerview != null){
mRecyclerview.setHasFixedSize(true);
}
mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL
);
mRecyclerview.setLayoutManager(mLayoutManager);
FirebaseRecyclerAdapter<Polls, PollHolder> mFireAdapter = new FirebaseRecyclerAdapter<Polls, PollHolder>(
Polls.class,
R.layout.latest_item,
PollHolder.class,
mPollsRef
){
@Override
protected void populateViewHolder(PollHolder viewHolder, Polls model, int position) {
viewHolder.mPollQuestion.setText(model.getQuestion());
Picasso.with(getActivity().getApplicationContext())
.load(model.getPollImage())
.fit()
.into(viewHolder.mPollImage);
}
};
mRecyclerview.setAdapter(mFireAdapter);
return v;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public static class PollHolder extends RecyclerView.ViewHolder {
TextView mPollQuestion;
ImageView mPollImage;
public PollHolder(View itemView) {
super(itemView);
mPollQuestion = (TextView) itemView.findViewById(R.id.latest_item_question);
mPollImage = (ImageView) itemView.findViewById(R.id.pollThumbNailImage);
}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
这是我收到的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.troychuinard.fanpolls, PID: 11738
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
at com.firebase.ui.database.FirebaseArray.<init>(FirebaseArray.java:43)
at com.firebase.ui.database.FirebaseRecyclerAdapter.<init>(FirebaseRecyclerAdapter.java:128)
at com.troychuinard.fanpolls.Fragment.LiveFragment$1.<init>(LiveFragment.java:103)
at com.troychuinard.fanpolls.Fragment.LiveFragment.onCreateView(LiveFragment.java:98)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:672)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:634)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:615)
at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2175)
at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1154)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1147)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1117)
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1412)
at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1520)
at android.view.View$PerformClick.run(View.java:21294)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
这可能确实是一个映射问题,但我希望确认。我还附上了Firebase数据库的结构: