改变图像颜色

时间:2018-05-14 11:34:25

标签: java android android-fragments colors nullpointerexception

我目前正在尝试更改图像视图的颜色。我试图从片段中做到这一点,它工作正常,直到我切换活动。当我返回到用于更改图像颜色的片段时,我收到此错误,

className={props.classes.appbaricon}

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

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

任何人都知道修复?只有当我切换活动时才会出现此错误,感谢您的回答。

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

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

1 个答案:

答案 0 :(得分:1)

TASK [print] ******************************************************************************************************************************************************************************************************** ok: [localhost] => (item=None) => { "msg": "syd1.pool.ntp.org" } ok: [localhost] => (item=None) => { "msg": "syd2.pool.ntp.org" } ok: [localhost] => (item=None) => { "msg": "syd2.pool.ntp.org" } ok: [localhost] => (item=None) => { "msg": "lon1.pool.ntp.org" } ok: [localhost] => (item=None) => { "msg": "lon2.pool.ntp.org" } ok: [localhost] => (item=None) => { "msg": "lon2.pool.ntp.org" } ok: [localhost] => (item=None) => { "msg": "ntp1.pool.ntp.org" } ok: [localhost] => (item=None) => { "msg": "ntp2.pool.ntp.org" } ok: [localhost] => (item=None) => { "msg": "ntp2.pool.ntp.org" } PLAY RECAP

中试试这样Context
fragment

比这样使用

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);
    }

}