[英]unfortunately the app has stopped when i am debugging if I cast PaintDrawable to Drawable
I'm studying and trying to make some Android application with android studio. 我正在研究并尝试使用android studio制作一些Android应用程序。 But I found something that I can't understand.
但是我发现了我无法理解的东西。
If I write the sentence below, there is no problem: 如果我写下面的句子,那没有问题:
TextView vm = new TextView(this);
TextView vm =新的TextView(this);
Drawable pnt = vm.getBackground();
Drawable pnt = vm.getBackground();
but, if I write a sentence like below, where I cast PaintDrawable
to the righthand, and build this app and install apk to my device, app doesn't work showing message "Unfortunately, the app has stopped". 但是,如果我写如下句子,将
PaintDrawable
到右侧,然后构建该应用程序并将apk安装到我的设备上,则该应用程序将无法工作,并显示消息“不幸的是,该应用程序已停止”。
TextView vm = new TextView(this);
TextView vm =新的TextView(this);
PaintDrawable pnt = (PaintDrawable) vm.getBackground();
PaintDrawable pnt =(PaintDrawable)vm.getBackground();
TextView::getBackground returns a Drawable , not necessarily a PaintDrawable. TextView :: getBackground 返回Drawable ,不一定是PaintDrawable。
You are attempting to downcast to PaintDrawable without checking whether you can do so. 您试图向下转换到PaintDrawable,而不检查是否可以这样做。 Try this:
尝试这个:
TextView vm = new TextView(this);
if (vm.getBackground() instanceof PainDrawable) {
PaintDrawable pnt = (PaintDrawable) vm.getBackground();
// ...
}
else {
Log.d("SomeTag", vm.getBackground().getClass());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.