更改图像颜色 [英] Changing Image Colour
本文介绍了更改图像颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试更改imageview的颜色。我正在尝试从一个片段开始执行此操作,在我切换活动之前,它工作正常。当我返回用于更改图像颜色的片段时,出现此错误,
I am currently trying to change the colour of an imageview. I am trying to do this from a fragment, and it works fine until I switch activities. When I return to the fragment that I use to change the colour of the image I get this error,
Attempt to invoke virtual method 'int android.content.Context.getColor(int)' on a null object reference
上的'int android.content.Context.getColor(int)'以下代码更改我图像的颜色,
I use the following code to change the colour of my image,
imgUpvote.setColorFilter(ContextCompat.getColor(getContext(), R.color.lGrey));
有人知道解决办法吗?仅当我切换活动时,才会出现此错误,谢谢您的回答。
Anyone know a fix? This error only occurs when I switch activities, thanks for any answers.
编辑:这是代码所在的位置,
here is where code is located,
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));
}
}
推荐答案
尝试这样会在您的片段
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));
这篇关于更改图像颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文