如何在我的主窗口中显示弹出窗口?
嗨,朋友们,我下载了一个弹出窗口示例源代码...它工作正常...当我运行此应用程序时,模拟器屏幕显示一个按钮,如果我单击底部显示的该按钮弹出窗口。如果我再次单击同一按钮,它将关闭弹出窗口...但是,
我希望当我的应用程序打开时我需要静态弹出窗口,不需要这个按钮。然后我点击软键盘(计算机键盘)f2按钮,我想关闭弹出窗口...这就是全部,如果有人知道请帮助我......
这是我的源代码:
package popupTest.popupTest;
import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
public class popupTest extends Activity {
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
mainLayout = new LinearLayout(this);
tv = new TextView(this);
but = new Button(this);
but.setText("Click Me");
but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
// popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(but, params);
setContentView(mainLayout);
}
}
logcat错误:
08-23 16:38:23.771: ERROR/AndroidRuntime(433): FATAL EXCEPTION: main
08-23 16:38:23.771: ERROR/AndroidRuntime(433): java.lang.RuntimeException: Unable to start activity ComponentInfo{popupTest.popupTest/popupTest.popupTest.popupTest}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.os.Looper.loop(Looper.java:123)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at java.lang.reflect.Method.invoke(Method.java:521)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at dalvik.system.NativeStart.main(Native Method)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.view.ViewRoot.setView(ViewRoot.java:505)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.widget.PopupWindow.invokePopup(PopupWindow.java:828)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:688)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at popupTest.popupTest.popupTest.onCreate(popupTest.java:49)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): ... 11 more
08-23 16:38:23.820: WARN/ActivityManager(59): Force finishing activity popupTest.popupTest/.popupTest
hi friends i download one popup window sample source code...its working fine...when i run this application emulator screen display one button, if i click that button popup window shown in bottom.if i click again same button its dismiss the popup window...but,
i expect when my application open i need static popup window, no need this button. then i click softkey board(computer keyboard)f2 button, the popup window i want to dismiss...thats all if anyone know please help me......
This is my source code:
package popupTest.popupTest;
import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
public class popupTest extends Activity {
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
mainLayout = new LinearLayout(this);
tv = new TextView(this);
but = new Button(this);
but.setText("Click Me");
but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
// popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(but, params);
setContentView(mainLayout);
}
}
logcat error:
08-23 16:38:23.771: ERROR/AndroidRuntime(433): FATAL EXCEPTION: main
08-23 16:38:23.771: ERROR/AndroidRuntime(433): java.lang.RuntimeException: Unable to start activity ComponentInfo{popupTest.popupTest/popupTest.popupTest.popupTest}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.os.Looper.loop(Looper.java:123)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at java.lang.reflect.Method.invoke(Method.java:521)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at dalvik.system.NativeStart.main(Native Method)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.view.ViewRoot.setView(ViewRoot.java:505)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.widget.PopupWindow.invokePopup(PopupWindow.java:828)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:688)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at popupTest.popupTest.popupTest.onCreate(popupTest.java:49)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): ... 11 more
08-23 16:38:23.820: WARN/ActivityManager(59): Force finishing activity popupTest.popupTest/.popupTest
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将代码放在
onCreate
中的onClick
之外 ....并使用setOnclickListener
删除弹出窗口..put the code outside
onClick
inonCreate
....and usesetOnclickListener
to remove ur popup..我已经编辑了您的代码,它将在活动开始时显示弹出窗口:
I have edited your code and it will show the pop up on the start of the activity: