[英]setVisibility(View.INVISIBLE) causes exception, why?
why my setVisibility(View.INVISIBLE) causes exception while setVisibility for TextView works.为什么我的 setVisibility(View.INVISIBLE) 在 TextView 的 setVisibility 工作时导致异常。 I also tried set it for ImageView and it also does not work - I am getting exception too
我也尝试为 ImageView 设置它,但它也不起作用 - 我也遇到了异常
public void alertdiag() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alertdiag_layout, null);
dialogBuilder.setView(dialogView).show();
View Divider1 = (View) dialogView.findViewById(R.style.Divider1);
Divider1.setVisibility(View.INVISIBLE); //causes java.lang.IllegalStateException: Could not execute method of the activity"
TextView HELP0 = (TextView) dialogView.findViewById(R.id.HELP0);
HELP0.setVisibility(View.INVISIBLE); // this works
}
alertdiag_layout.xml: alertdiag_layout.xml:
<View style="@style/Divider1"
android:layout_below="@+id/HELP3"
android:background="#000000"
android:layout_alignRight="@+id/HELP_FIX_LINE"
android:layout_alignLeft="@+id/HELP3"
/>
<TextView
android:id="@+id/HELP0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/HELP_FIX_LINE"
android:textStyle="bold"
android:typeface="normal"
/>
styles.xml:样式.xml:
<style name="Divider1">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
</style>
You are trying to set the Visibility to a Style (R.style.Divider1)您正在尝试将可见性设置为样式 (R.style.Divider1)
Add an Id to your View and change the R.style.Divider1 to R.id.yourNewId将 Id 添加到您的视图并将 R.style.Divider1 更改为 R.id.yourNewId
<View style="@style/Divider1" android:id="@+id/yourNewId"
android:layout_below="@id/HELP3" android:background="#000000"
android:layout_alignRight="@id/HELP_FIX_LINE"
android:layout_alignLeft="@id/HELP3" />
Add id attribute to your view将 id 属性添加到您的视图
<View style="@style/Divider1"
android:id="@+id/my_view"
android:layout_below="@id/HELP3"
android:background="#000000"
android:layout_alignRight="@id/HELP_FIX_LINE"
android:layout_alignLeft="@id/HELP3"
/>
NOTE: I switched @+id/HELP3 and @+id/HELP_FIX_LINE with @id/HELP3 and @id/HELP_FIX_LINE respectively because @+id/ is only used when you are giving a view an id.注意:我将@+id/HELP3 和@+id/HELP_FIX_LINE 分别与@id/HELP3 和@id/HELP_FIX_LINE 切换,因为@+id/ 仅在您为视图提供id 时使用。 It is not used to refer other views.
它不用于引用其他视图。
Now, Change this line of code现在,更改这行代码
View Divider1 = (View) dialogView.findViewById(R.style.Divider1);
with和
View Divider1 = (View) dialogView.findViewById(R.id.my_view);
It will find the view with the given id, instead of style.它将找到具有给定 id 的视图,而不是样式。
To make the view invisible you need to set要使视图不可见,您需要设置
Divider1.setVisibility(View.INVISIBLE);
setVisibility must be used on the view, not on the style. setVisibility 必须用于视图,而不是样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.