在片段中使用颜色选择器更改EditText的背景颜色

3
我想使用颜色选择器更改我的编辑文本的背景颜色。我有一个类,在其中有一个editText。我正在使用片段,在该片段中,使用该类的特定editText。 在片段中,有一个图像视图,点击它将显示颜色选择器对话框。
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);
   EditText et = (EditText) view.findViewById(R.id.txtTest);

   ImageView imgChooser = (ImageView) view.findViewById(R.id.imgPallete);

}

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    imgChooser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AmbilWarnaDialog colorpicker = new AmbilWarnaDialog(getContext(), Color.BLACK, new AmbilWarnaDialog.OnAmbilWarnaListener() {

                @Override
                public void onCancel(AmbilWarnaDialog dialog) {

                }

                @Override
                public void onOk(AmbilWarnaDialog dialog, int color) {
                    et.setBackgroundColor(color);
                }
            });
            colorpicker.show();
        }
    });

}

但是这个不起作用,显示 java.lang.NullPointerException。有人可以帮忙吗?我刚刚开始学习 Android Studio。

错误日志如下

java.lang.NullPointerException
                                                                                                 at com.example.sudeepbajracharya.myapplication.BackGround$1$1.onOk(BackGround.java:61)
                                                                                                 at yuku.ambilwarna.AmbilWarnaDialog$6.onClick(AmbilWarnaDialog.java:179)
                                                                                                 at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:169)
                                                                                                 at android.os.Handler.dispatchMessage(Handler.java:107)
                                                                                                 at android.os.Looper.loop(Looper.java:194)
                                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5371)
                                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                                 at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                                                                                                 at dalvik.system.NativeStart.main(Native Method)

问题显示在第61行,而在BackGround.java:61中有:
et.setBackgroundColor(color);


java.lang.NullPointerException 请在提问时附上完整的崩溃日志。 - AskNilesh
@Nilu 我已经分享了我的崩溃日志,请查看一下。 :) 谢谢 - Sudeep
请分享完整的活动代码和XML。 - Syed Hamza Hassan
1
你在onViewCreated中初始化的et变量是局部变量。由于你没有提供整个代码,我假设全局声明的et变量没有被初始化,因此导致了NPE错误。 - Rajen Raiyarela
显示剩余2条评论
2个回答

0

你的 EditText 没有正确引用,请尝试以下代码

private EditText et;
private ImageView imgChooser;

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);
   et = (EditText) view.findViewById(R.id.txtTest);

   imgChooser = (ImageView) view.findViewById(R.id.imgPallete);

}

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    imgChooser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AmbilWarnaDialog colorpicker = new AmbilWarnaDialog(getContext(), Color.BLACK, new AmbilWarnaDialog.OnAmbilWarnaListener() {

                @Override
                public void onCancel(AmbilWarnaDialog dialog) {

                }

                @Override
                public void onOk(AmbilWarnaDialog dialog, int color) {
                    et.setBackgroundColor(color);
                }
            });
            colorpicker.show();
        }
    });

}

谢谢你的回复,兄弟,但它仍然不起作用 :( 我在 et.setBackgroundColor(color) 遇到问题。 - Sudeep

0

你可以使用

edittext.setBackgroundResource(R.color.white);

如果上面的代码片段不起作用,您可以尝试下面的代码

如果您在活动中使用,请使用此代码

edittext.setBackground(ContextCompat.getColor(this,R.color.yourcolor));

如果您正在Fragment中使用,可以使用以下代码:

edittext.setBackground(ContextCompat.getColor(getActivity(),R.color.yourcolor));

如果你在RecyclerView适配器中,请使用以下代码

edittext.setBackground(ContextCompat.getColor(context,R.color.yourcolor));

因为edittext.setBackgroundColor()需要一个数字形式的颜色(例如,红色为0xFFFF0000)。而R.color.white也是一个数字。

同时确保你正确地引用了edittext


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接