我收到一条错误消息,解释了在添加内容之前必须先调用requestFeature()。因此,我做到了这一点,这使该对话框在第一次单击时起作用,但是在第二次单击时出现相同的错误。
Logcat
at android.view.View$DeclaredOnClickListener.onClick(View.java:5664)
at android.view.View.performClick(View.java:6877)
at android.view.View$PerformClick.run(View.java:26069)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.PhoneWindow.requestFeature(PhoneWindow.java:385)
at uk.co.controlf1.notifyv2.ShortCodeActivity.ShowPopup(ShortCodeActivity.java:197)
ShowPopup方法
public void ShowPopup(View v) {
myDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
myDialog.setContentView(R.layout.more_info_dialog);
txtclose = myDialog.findViewById(R.id.txtclose);
txtclose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myDialog.dismiss();
}
});
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.show();
}
主要xml
<TextView
android:id="@+id/errorMessage"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_below="@id/submitShortCodeBtn"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Please enter a value"
android:textColor="#084c5c"
android:textSize="17dp"
android:visibility="invisible" />
<ImageButton
android:id="@+id/btn_ic_info"
android:layout_width="13dp"
android:layout_height="13dp"
android:layout_alignTop="@+id/errorMessage"
android:layout_alignEnd="@+id/errorMessage"
android:layout_alignRight="@+id/errorMessage"
android:backgroundTint="#fff"
android:padding="0dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_info"
android:visibility="invisible"
android:onClick="ShowPopup"
/>
对话框xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="200dp"
android:background="@drawable/dialog_background"
android:padding="15dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="60dp">
<TextView
android:id="@+id/txtclose"
android:layout_width="30dp"
android:layout_height="30dp"
android:textColor="#084c5c"
android:text="X"
android:textStyle="bold"
android:layout_alignParentRight="true"
android:gravity="center"
/>
<TextView
android:id="@+id/more_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="More Info"
android:textColor="#fff"
android:textSize="24dp"
android:layout_marginBottom="10dp"
android:layout_centerHorizontal="true"
android:textStyle="bold"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/shortcode_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is a shortcode?"
android:textColor="#084c5c"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="@+id/shortcode_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/shortcode_question"
android:text="A shortcode is...."
android:textSize="12dp"
android:textStyle="italic" />
<TextView
android:id="@+id/forgot_pass_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/shortcode_answer"
android:layout_marginTop="5dp"
android:text="What if I have forgot my shortcode?"
android:textColor="#084c5c"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="@+id/forgot_pass_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/forgot_pass_question"
android:text="Contact..."
android:textSize="12dp"
android:textStyle="italic" />
<TextView
android:id="@+id/internet_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/forgot_pass_answer"
android:layout_marginTop="5dp"
android:text="Why do I need an internet connection?"
android:textColor="#084c5c"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="@+id/internet_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/internet_question"
android:text="You need an internet connection to..."
android:textSize="12dp"
android:textStyle="italic" />
<TextView
android:id="@+id/support_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/internet_answer"
android:layout_marginTop="5dp"
android:text="Why do I need an internet connection?"
android:textColor="#084c5c"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="@+id/suport_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/support_question"
android:text="You need an internet connection to..."
android:textSize="12dp"
android:textStyle="italic" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
我正在主XML中设置onclick,不确定是否与此有关。