如何在我的主窗口中显示弹出窗口?

时间:2011-08-23 10:45:38

标签: java android popup window

朋友们,我下载一个弹出窗口示例源代码...它的工作正常...当我运行此应用程序模拟器屏幕显示一个按钮,如果我点击底部显示的那个按钮弹出窗口。如果我再次点击相同的按钮它取消了弹出窗口......但是,

我希望当我的应用程序打开时我需要静态弹出窗口,不需要这个按钮。然后我点击软键盘(电脑键盘)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

2 个答案:

答案 0 :(得分:0)

将代码放在onClick以外的onCreate ....并使用setOnclickListener删除弹出广告...

答案 1 :(得分:0)

我已编辑了您的代码,它会在活动开始时显示弹出窗口:

 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");



  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);

    Handler handler = new Handler();
      handler.postDelayed(new Runnable(){

        public void run() {
            // TODO Auto-generated method stub
            popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
             popUp.update(50, 50, 300, 80);
        }

      }, 1000);
  //Use this to dismiss as per your need...
    // popUp.dismiss();

 }
 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
         popUp.dismiss();
        return false;

    }
}