开发者

how to show popup window in my main window?

开发者 https://www.devze.com 2023-03-29 20:52 出处:网络
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

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.performLa开发者_如何学PythonunchActivity(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


put the code outside onClick in onCreate ....and use setOnclickListener to remove ur popup..


I have edited your code and it will show the pop up on the start of the activity:

 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;

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消