简体   繁体   中英

I get a No view for id for fragment

I am trying to have an activity inside of my fragment, the fragment shows posts of people you follow and the activity shows the most recent post from anyone.

My fragment..

 public class HomeFragment extends Fragment {

    private PostAdapter postAdapter;
    private List<Post> postLists;

    private List<String> followingList;

    private ProgressBar progressBar;

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

        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        linearLayoutManager.setReverseLayout(true);
        linearLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        postLists=new ArrayList<>();
        postAdapter=new PostAdapter(getContext(), postLists);
        recyclerView.setAdapter(postAdapter);

        ImageView globalPosts = view.findViewById(R.id.globalPosts);

        globalPosts.setOnClickListener(v->{
            Intent intent= new Intent(getContext(), GlobalPostsActivity.class);
            startActivity(intent);
        });

        progressBar=view.findViewById(R.id.progress_circular);

        checkFollowing();

        return view;
    }

    private void checkFollowing(){
        followingList=new ArrayList<>();

        DatabaseReference reference=FirebaseDatabase.getInstance().getReference("Support")
                .child(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid())
                .child("supporting");

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                followingList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    followingList.add(snapshot.getKey());
                }
                readPosts();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    private void readPosts(){
        DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Posts");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                postLists.clear();
                for (DataSnapshot snapshot :dataSnapshot.getChildren()){
                    Post post=snapshot.getValue(Post.class);
                    for (String id: followingList){
                        assert post != null;
                        if (post.getPublisher().equals(id)){
                            postLists.add(post);
                        }
                    }
                }
                postAdapter.notifyDataSetChanged();
                progressBar.setVisibility(View.GONE);
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
    }
}

My activity..

public class GlobalPostsActivity extends AppCompatActivity {

    private PostAdapter postAdapter;
    private List<Post> postLists;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_global_posts);

        RecyclerView recyclerView = findViewById(R.id.recycler_view1);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setReverseLayout(true);
        linearLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        postLists=new ArrayList<>();
        postAdapter=new PostAdapter(this, postLists);
        recyclerView.setAdapter(postAdapter);

        readGlobalPosts();

    }
    private void readGlobalPosts(){
        DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Posts");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                postLists.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    Post post=snapshot.getValue(Post.class);
                    assert post !=null;
                    postLists.add(post);
                }
                postAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
    }
}

I get a fatal exception: main and my app crashes when i click on a users name to go to their profile. I am not sure why this is happening so any help is greatly appreciated.

There is one bit of your code I can see that will crash if asserts are enabled:

            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Post post=snapshot.getValue(Post.class);
                assert post !=null; // THIS
                postLists.add(post);
            }

What you have written there is the equivalent of:

            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Post post=snapshot.getValue(Post.class);
                if(post == null) {
                    throw new IllegalStateException("Post was null");
                }
                postLists.add(post);
            }

and I am sure you don't want to crash your whole app when there is no Post?

If you want to test if it exists (and get feedback if it doesn't), try something like this:

            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Post post=snapshot.getValue(Post.class);
                if(post == null) {
                    Log.w("TUT", "Expected a post and didn't get one.");
                } else {
                    postLists.add(post);
                }
            }

Ref:

https://en.wikibooks.org/wiki/Java_Programming/Keywords/assert How to use assert in android?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM