gpt4 book ai didi

java - 改变图像颜色

转载 作者:行者123 更新时间:2023-12-02 11:15:11 25 4
gpt4 key购买 nike

我目前正在尝试更改 ImageView 的颜色。我正在尝试从一个 fragment 中执行此操作,并且在我切换 Activity 之前它工作正常。当我返回到用于更改图像颜色的 fragment 时,出现此错误,

Attempt to invoke virtual method 'int android.content.Context.getColor(int)' on a null object reference

我使用以下代码来更改图像的颜色,

imgUpvote.setColorFilter(ContextCompat.getColor(getContext(), R.color.lGrey));

有人知道解决方法吗?仅当我切换 Activity 时才会出现此错误,感谢您的回答。

编辑:这里是代码所在的位置,

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<postsGetInfo, postsGetInfoViewHolder>(postsGetInfo.class, R.layout.posts_layout,postsGetInfoViewHolder.class,postRef) {
@Override
protected void populateViewHolder(final postsGetInfoViewHolder viewHolder, postsGetInfo model, int position) {
final String postKey = getRef(position).getKey();
UpdateTheDisplayVotes(postKey); //Displays the votes at the start of creation
postRef.removeEventListener(VoteListener);

defaultVote = VotesRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.hasChild(postKey)) {
VotesRef.child(postKey).child(current_user_id).child("votes").setValue("none");
}
String voteStatus = dataSnapshot.child(postKey).child(current_user_id).child("votes").getValue().toString();
if (voteStatus.equals("upvoted")){
ImageView btnUpvote = viewHolder.myView.findViewById(R.id.imgUpvote);
ImageView btnDownvote = viewHolder.myView.findViewById(R.id.imgDownvote);
btnUpvote.setColorFilter(ContextCompat.getColor(getActivity(), R.color.lBlue));
btnDownvote.setColorFilter(ContextCompat.getColor(getActivity(), R.color.lGrey));
}if (voteStatus.equals("downvoted")){
ImageView btnUpvote = viewHolder.myView.findViewById(R.id.imgUpvote);
ImageView btnDownvote = viewHolder.myView.findViewById(R.id.imgDownvote);
btnUpvote.setColorFilter(ContextCompat.getColor(getActivity(), R.color.lGrey));
btnDownvote.setColorFilter(ContextCompat.getColor(getActivity(), R.color.Black));
}if (voteStatus.equals("none")){
ImageView btnUpvote = viewHolder.myView.findViewById(R.id.imgUpvote);
ImageView btnDownvote = viewHolder.myView.findViewById(R.id.imgDownvote);
btnUpvote.setColorFilter(ContextCompat.getColor(getActivity(), R.color.lGrey));
btnDownvote.setColorFilter(ContextCompat.getColor(getActivity(), R.color.lGrey));
}
}

最佳答案

尝试在您的 fragment 中获取Context,如下所示

public class BlankFragment extends Fragment {


public BlankFragment() {

}

Context mContext;

@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment


return inflater.inflate(R.layout.fragment_blank, container, false);
}

}

而不是像这样使用

kimgUpvote.setColorFilter(ContextCompat.getColor(mContext, R.color.lGrey));

关于java - 改变图像颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50329302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com