我创建了一个带有按钮的类,该按钮显示了库中定义的弹出窗口。弹出窗口有一个关闭按钮。当我点击关闭按钮时,它会尝试执行dismiss方法,但它不起作用。 我想知道为什么这种方法(解雇)不起作用。
MainActivity.java
package com.personal.testfeedback;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import com.example.testfeedback.R;
import com.personal.feedback.Feedback;
public class MainActivity extends Activity {
Button btnCreatePopup;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout parent = (RelativeLayout)findViewById(R.id.layout);
btnCreatePopup = (Button) findViewById(R.id.button1);
btnCreatePopup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Feedback feedback = new Feedback(MainActivity.this);
feedback.initiatePopupWindow(parent);
}
});
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFF000" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
Feedback.java
package com.personal.feedback;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
public class Feedback extends Activity {
private Context context;
Button btnClosePopup;
protected PopupWindow pwindo;
public Feedback(Context ctx) {
this.context = ctx;
}
public void initiatePopupWindow(RelativeLayout parent) {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup, parent);
pwindo = new PopupWindow(layout, 600, 500, true);
pwindo.setFocusable(true);
// pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button_click_listener = new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
return ;
}
};
}
screen_popup.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal|center_vertical">
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="Hello!" />
<Button
android:id="@+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>
</RelativeLayout>
如果我不评论行:pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
出现以下错误:
01-14 16:27:28.149: W/System.err(5515): java.lang.RuntimeException: view android.widget.RelativeLayout@405b49d0 being added, but it already has a parent
01-14 16:27:28.149: W/System.err(5515): at android.view.View.assignParent(View.java:6064)
01-14 16:27:28.159: W/System.err(5515): at android.view.ViewRoot.setView(ViewRoot.java:573)
01-14 16:27:28.169: W/System.err(5515): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
01-14 16:27:28.169: W/System.err(5515): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-14 16:27:28.169: W/System.err(5515): at android.view.Window$LocalWindowManager.addView(Window.java:424)
01-14 16:27:28.179: W/System.err(5515): at android.widget.PopupWindow.invokePopup(PopupWindow.java:907)
01-14 16:27:28.179: W/System.err(5515): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:767)
01-14 16:27:28.179: W/System.err(5515): at com.personal.feedback.Feedback.initiatePopupWindow(Feedback.java:35)
01-14 16:27:28.179: W/System.err(5515): at com.personal.testfeedback.MainActivity$1.onClick(MainActivity.java:30)
01-14 16:27:28.179: W/System.err(5515): at android.view.View.performClick(View.java:2494)
01-14 16:27:28.179: W/System.err(5515): at android.view.View$PerformClick.run(View.java:9109)
01-14 16:27:28.179: W/System.err(5515): at android.os.Handler.handleCallback(Handler.java:587)
01-14 16:27:28.179: W/System.err(5515): at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 16:27:28.189: W/System.err(5515): at android.os.Looper.loop(Looper.java:130)
01-14 16:27:28.189: W/System.err(5515): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-14 16:27:28.189: W/System.err(5515): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 16:27:28.189: W/System.err(5515): at java.lang.reflect.Method.invoke(Method.java:507)
01-14 16:27:28.189: W/System.err(5515): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
01-14 16:27:28.189: W/System.err(5515): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
01-14 16:27:28.189: W/System.err(5515): at dalvik.system.NativeStart.main(Native Method)
01-14 16:27:45.739: D/AndroidRuntime(5515): Shutting down VM
01-14 16:27:45.739: E/AndroidRuntime(5515): Uncaught handler: thread main exiting due to uncaught exception
01-14 16:27:45.739: W/dalvikvm(5515): threadid=1: thread exiting with uncaught exception (group=0x400ad560)
01-14 16:27:45.749: E/Monkey(5515): exception :java.io.FileNotFoundException: /dev/kmsg (Permission denied)
01-14 16:27:45.779: W/System.err(5515): java.io.IOException: Permission denied
01-14 16:27:45.809: E/AndroidRuntime(5515): exception :java.io.FileNotFoundException: /dev/kmsg (Permission denied)
01-14 16:27:45.809: E/AndroidRuntime(5515): FATAL EXCEPTION: main
01-14 16:27:45.809: E/AndroidRuntime(5515): java.lang.ClassCastException: android.view.WindowManager$LayoutParams
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3270)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.view.View.measure(View.java:8342)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.view.View.measure(View.java:8342)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3279)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.view.View.measure(View.java:8342)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.view.ViewRoot.performTraversals(ViewRoot.java:841)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.view.ViewRoot.handleMessage(ViewRoot.java:1876)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.os.Looper.loop(Looper.java:130)
01-14 16:27:45.809: E/AndroidRuntime(5515): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-14 16:27:45.809: E/AndroidRuntime(5515): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 16:27:45.809: E/AndroidRuntime(5515): at java.lang.reflect.Method.invoke(Method.java:507)
01-14 16:27:45.809: E/AndroidRuntime(5515): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
01-14 16:27:45.809: E/AndroidRuntime(5515): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
01-14 16:27:45.809: E/AndroidRuntime(5515): at dalvik.system.NativeStart.main(Native Method)
01-14 16:27:45.829: E/AndroidRuntime(5515): exception :java.io.FileNotFoundException: /dev/kmsg (Permission denied)
01-14 16:27:45.839: W/System.err(5515): java.io.FileNotFoundException: /data/plog.log (Permission denied)
此致,Alek
答案 0 :(得分:0)
你混淆了代码中的东西。
首先,我也不明白你的反馈类扩展Activity的原因。它是一个帮助类,与Activity无关。
其次,在解除pwindo.showAtLocation时你的错误是由于你对inflater.inflate(R.layout.screen_popup,parent)的调用给了screen_popup中的RelativeLayout一个父类,所以使用showAtLocation你试图分配父母再次使用相同的资源。
提出你的问题,你打开弹出窗口的方式有点奇怪。 事实上,您正在传递MainActivity的上下文并且正在为新布局充气。 因此,您在GUI上看到的不是上一个窗口的新窗口,而是前一个具有不同布局的窗口。您可以在显示弹出窗口时按Android设备上的后退按钮看到:您没有移动到上一个屏幕,但只是退出应用程序。
因此,当您创建PopupWindow时,您没有使用它(因为您没有调用它的showAtLocation方法),因此对其dismiss()方法的调用不提供任何效果。 要关闭窗口,您必须在上下文中调用finish()方法(您必须将其强制转换为MainActivity):
((MainActivity)context).finish()
但是我知道这不是你要做的,因为它没有关闭弹出窗口,而是整个应用程序。 要获得弹出效果,请试试这个鳕鱼:
public class Feedback {
private Context context;
private MainActivity parent;
Button btnClosePopup;
protected PopupWindow pwindo;
public Feedback(MainActivity _parent) {
parent = _parent;
}
public void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
RelativeLayout viewGroup = (RelativeLayout) parent
.findViewById(R.id.popup_element);
LayoutInflater inflater = (LayoutInflater) parent
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup, viewGroup);
pwindo = new PopupWindow(layout, 600, 500, true);
pwindo.setFocusable(true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button_click_listener = new OnClickListener()
public void onClick(View v) {
pwindo.dismiss();
return;
}
};
}