适配器中的ImageView由于某些原因为空

时间:2019-06-20 15:20:40

标签: android firebase firebase-realtime-database

因此,在适配器的getView方法中,当我尝试使用指向布局上的ImageView的指针时,该指针为null

这是适配器类:

public class PostAdapter extends ArrayAdapter<PostClass> {

    private Context context;
    private int resource;
    private ArrayList<PostClass> objects;
    private HashMap<String, String> poster = new HashMap<>();


    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference ref = database.getReference();
    DatabaseReference usersReference = ref.child("users");

    //DatabaseReference eventsReference = ref.child("Events");


    public PostAdapter(Context context, int resource, ArrayList<PostClass> objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        this.objects = objects;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        String postId = objects.get(position).getPostId();
        LayoutInflater inflater = LayoutInflater.from(context);

        convertView = inflater.inflate(resource, parent, false);


        ImageView postImage = convertView.findViewById(R.id.postImgShow);
        TextView goingTxt = convertView.findViewById(R.id.postGoing);
        TextView description = convertView.findViewById(R.id.postDescriptionShow);
        ImageView posterImage = convertView.findViewById(R.id.publisherPic);
        TextView posterName = convertView.findViewById(R.id.publisherName);
        ImageView joinSignal = convertView.findViewById(R.id.joinSignal);

        if(postImage == null){
            Toast.makeText(context, "Is Null!!!!!!!!!!", Toast.LENGTH_SHORT).show();
            Log.i("IsNull","yes");
        }

        publisherInfo(position, posterImage, posterName);

        Picasso.get().load(objects.get(position).getPostImage()).fit().centerCrop().into(postImage);
        goingTxt.setText(objects.get(position).getGoing() + " going");
        description.setText(objects.get(position).getPostDescription());
        boolean bool = viewerGoing(postId);
        if (bool)
            joinSignal.setImageResource(R.drawable.ic_added);
        return convertView;
    }


    private void publisherInfo(final int position, final ImageView posterImage, final TextView posterName) {

        DatabaseReference posterRef;
        posterRef = usersReference.child(objects.get(position).getPostPublisher());
        posterRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                UserClass thisPoster = dataSnapshot.getValue(UserClass.class);


                Picasso.get().load(thisPoster.getPic()).fit().centerCrop().into(posterImage);
                posterName.setText(thisPoster.getName());

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                System.out.println("The read failed: " + databaseError.getCode());
            }
        });

    }

    private boolean viewerGoing(String postId) {
        boolean ret = false;
        return ret;
    }
}

这是我要更改的listView片段:

public class PostsFragment extends Fragment {

    private FloatingActionButton fab;
    private ListView listView;
    private PostAdapter adapter;
    private ArrayList<PostClass> posts;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.posts_layout,container, false);
        posts = new ArrayList<>();
        fab = view.findViewById(R.id.fab);
        listView = view.findViewById(R.id.posts);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), PostActivity.class);
                startActivity(intent);
            }
        });

        readPosts();
        adapter = new PostAdapter(getContext(), R.layout.posts_layout, posts);
        listView.setAdapter(adapter);
        return view;
    }
    private void readPosts(){
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Posts");

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                posts.clear();
                for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                    posts.add(snapshot.getValue(PostClass.class));
                }
                adapter.notifyDataSetChanged();
            }

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

            }
        });
    }
}

当我尝试使用毕加索将图像加载到ImageView时,它说目标为空(而且我也可以在日志中看到它说是)

2 个答案:

答案 0 :(得分:0)

一旦创建了片段,就会调用

onCreateView,以便您可以创建其用户界面。

一旦创建了片段及其父级“视图”,就会触发

onViewCreated。 因此在onViewCreated回调中,您应该完成片段初始化。 (任何需要创建父活动的内容。)

有关更多信息,请参见Fragment Lifecycle

答案 1 :(得分:0)

要做:

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.posts_layout, null);
    }

而不是:

    LayoutInflater inflater = LayoutInflater.from(context);

    convertView = inflater.inflate(resource, parent, false);

然后,您不必将引用从父级作为资源ID传递给布局。我怀疑这就是问题所在。

这还假设R.layout.posts_layout是列表项的布局,而不是列表布局。两件事。