【问题标题】:EditText Backcolor crashesEditText 背景色崩溃
【发布时间】:2019-11-09 16:29:04
【问题描述】:

谁能告诉我为什么这个简单的程序总是崩溃?

    EditText editText1 = (EditText) findViewById(R.id.editText1);
    View.OnClickListener onClickListener_button1 = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            ColorDrawable drawable = (ColorDrawable)editText1.getBackground();

        }

    };

【问题讨论】:

  • 你看过Android Studio底部的Logcat吗?我的猜测是您实际上在 editText1 的背景中没有 ColorDrawable,因此转换失败。
  • java.lang.ClassCastException: android.graphics.drawable.InsetDrawable 不能转换为 android.graphics.drawable.ColorDrawable
  • 所以我的假设是正确的,你有 InsetDrawable 而不是 ColorDrawable。你想用drawable做什么?也许 InsetDrawable 可以做你需要做的事情。
  • 谢谢!至少使用 InsetDrawable 不会压垮应用。我最初的意图是首先获取edittext当前背景颜色,然后根据我选择的颜色进行更改。
  • Szymon,非常感谢!似乎它确实开始工作了。我怎样才能在这里提高你的地位? :)

标签: android android-edittext colordrawable


【解决方案1】:

正如在 cmets 中发现的那样,您将 InsetBackground 转换为 ColorBackground,但失败了。

要获得正确的结果,您需要将android:background="@color/initial_color" 设置为xml 中的EditText。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 2014-11-08
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多