无法从firebase数据库获取数据

l3zydbqr  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(209)

我正在创建一个应用程序,现在我正在使用recyclerview显示存储在firebase数据库中的所有用户帖子。但我有一个问题,当程序从名为location、title和days的字符串接收数据时,会抛出空指针异常。您可以在下面看到代码:
后内容:

public class PostContent {
    String days, description, difficulty, imageUri, kilometers, location, map, nickname,
    time, title, uid;

    Long numOfImages;

    public PostContent()
    {
    }

    public PostContent(String days, String description, String difficulty, String imageUri, String kilometers, String location, String map, String nickname, String time, String title, String uid, Long numOfImages) {
        this.days = days;
        this.description = description;
        this.difficulty = difficulty;
        this.imageUri = imageUri;
        this.kilometers = kilometers;
        this.location = location;
        this.map = map;
        this.nickname = nickname;
        this.time = time;
        this.title = title;
        this.uid = uid;
        this.numOfImages = numOfImages;
    }

    public String getDays() {
        return days;
    }

    public void setDays(String days) {
        this.days = days;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDifficulty() {
        return difficulty;
    }

    public void setDifficulty(String difficulty) {
        this.difficulty = difficulty;
    }

    public String getImageUri() {
        return imageUri;
    }

    public void setImageUri(String imageUri) {
        this.imageUri = imageUri;
    }

    public String getKilometers() {
        return kilometers;
    }

    public void setKilometers(String kilometers) {
        this.kilometers = kilometers;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getMap() {
        return map;
    }

    public void setMap(String map) {
        this.map = map;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public Long getNumOfImages() {
        return numOfImages;
    }

    public void setNumOfImages(Long numOfImages) {
        this.numOfImages = numOfImages;
    }

postsadapter:在这里您可以看到注解掉的代码,因为从数据库接收数据时问题就出现在这些行中

public class PostAdapter extends FirebaseRecyclerAdapter<PostContent, PostAdapter.postsViewHolder> {
    private Context mContext;

    public PostAdapter(FirebaseRecyclerOptions<PostContent> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull PostAdapter.postsViewHolder holder, int position, @NonNull PostContent model) {
        holder.nickname.setText(model.getNickname());
        holder.country.setText(model.getLocation());
        //holder.title.setText(model.getTitle());
        //holder.days.setText(model.getDays());
        holder.difficulty.setText(model.getDifficulty());
        //holder.kilometers.setText(model.getKilometers());
        holder.description.setText(model.getDescription());
        Picasso.get().load(model.getImageUri()).into(holder.avatarUri);

    }

    @NonNull
    @Override
    public PostAdapter.postsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.posts, parent, false);
        return new PostAdapter.postsViewHolder(view);
    }

    class postsViewHolder extends  RecyclerView.ViewHolder
    {
        CircleImageView avatarUri;
        TextView nickname, country, title, days, difficulty, likesNum, kilometers,description;
        SliderView mSliderView;
        ImageView likes, comments, save;
        ImageButton more;

        public postsViewHolder(@NonNull View view)
        {
            super(view);

            avatarUri = view.findViewById(R.id.post_profile_picture);
            nickname = view.findViewById(R.id.post_nickname);
            country = view.findViewById(R.id.post_country);
            title = view.findViewById(R.id.post_title);
            days = view.findViewById(R.id.post_days);
            difficulty = view.findViewById(R.id.post_difficulty);
            mSliderView = view.findViewById(R.id.images_posts);
            likes = view.findViewById(R.id.post_likes);
            comments = view.findViewById(R.id.post_comments);
            save = view.findViewById(R.id.post_save);
            likesNum = view.findViewById(R.id.post_likes_number);
            description = view.findViewById(R.id.post_description);
            more = view.findViewById(R.id.post_more);
            kilometers = view.findViewById(R.id.post_distance);
        }
    }
}

homefragment:

public class HomeFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private Context mContext;
    private FirebaseMethods mFirebaseMethods;
    private List<PostContent> mPostContentList;
    private PostAdapter mPostAdapter;
    private DatabaseReference postRef;
    private FirebaseAuth mAuth;
    private String currentUserID;

    public HomeFragment() {

    }

    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        mContext = container.getContext();
        mFirebaseMethods = new FirebaseMethods(mContext);
        mRecyclerView = view.findViewById(R.id.posts_recycler);
        DatabaseReference postRef = FirebaseDatabase.getInstance().getReference("Posts");

        postRef = FirebaseDatabase.getInstance().getReference().child("Posts");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        FirebaseRecyclerOptions options =
                new FirebaseRecyclerOptions.Builder<PostContent>()
                .setQuery(FirebaseDatabase.getInstance().getReference().child("Posts"), PostContent.class)
                .build();

        mPostAdapter = new PostAdapter(options);
        mRecyclerView.setAdapter(mPostAdapter);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        mPostAdapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        mPostAdapter.stopListening();
    }
}

firebase数据库:
结构
日志:

Process: com.company.android.myapplication, PID: 6989
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.company.android.myapplication.PostAdapter.onBindViewHolder(PostAdapter.java:41)
        at com.company.android.myapplication.PostAdapter.onBindViewHolder(PostAdapter.java:28)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
        at android.view.View.layout(View.java:22114)
        at android.view.ViewGroup.layout(ViewGroup.java:6335)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:334)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22114)
        at android.view.ViewGroup.layout(ViewGroup.java:6335)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:334)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22114)
        at android.view.ViewGroup.layout(ViewGroup.java:6335)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
        at android.view.View.layout(View.java:22114)
        at android.view.ViewGroup.layout(ViewGroup.java:6335)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:334)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22114)
        at android.view.ViewGroup.layout(ViewGroup.java:6335)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1839)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1683)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1592)
        at android.view.View.layout(View.java:22114)
        at android.view.ViewGroup.layout(ViewGroup.java:6335)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:334)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22114)
        at android.view.ViewGroup.layout(ViewGroup.java:6335)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1839)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1683)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1592)
        at android.view.View.layout(View.java:22114)
        at android.view.ViewGroup.layout(ViewGroup.java:6335)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:334)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:937)
        at android.view.View.layout(View.java:22114)
        at android.view.ViewGroup.layout(ViewGroup.java:6335)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3282)
2021-07-27 16:08:54.344 6989-6989/com.company.android.myapplication E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2782)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1854)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8086)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1057)
        at android.view.Choreographer.doCallbacks(Choreographer.java:875)
        at android.view.Choreographer.doFrame(Choreographer.java:776)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1042)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题