代码之家  ›  专栏  ›  技术社区  ›  user3839646

弹出窗口在我的活动中不起作用

  •  0
  • user3839646  · 技术社区  · 10 年前

    我在一个应用程序中实现了弹出窗口。我有示例代码 This 。如果我在我的活动中实现了此代码,那么它就不起作用了。所以我找不到我的错误。有人能帮我吗。

    解锁hud.java:

    inf = (LayoutInflater) getApplicationContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        myview = inf.inflate(R.layout.unlock_launcher, null);
        receive = (Button) myview.findViewById(R.id.receive1);
        reject = (Button) myview.findViewById(R.id.reject12);
        slidehandlebutton= (Button) myview.findViewById(R.id.slidehandlebutton);
        slidehandlebutton.setOnClickListener(this);
    
    
        Handler ha = new Handler();
        ha.postDelayed(new Runnable() {
    
            @Override
            public void run() {
                ActivityManager mActivityManager = (ActivityManager) getSystemService("activity");
                List<RunningTaskInfo> list_running = mActivityManager
                        .getRunningTasks(2147483647);
                List<RecentTaskInfo> list_recent = mActivityManager
                        .getRecentTasks(2147483647, 1);
    
                Log.d("main", "Running: " + list_running.get(0).topActivity);
                Log.d("main", "Recent: " + list_recent.get(0).baseIntent);
            /*  Intent homeIntent = new Intent(Intent.ACTION_MAIN);
                homeIntent.addCategory(Intent.CATEGORY_HOME);
                homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
                startActivity(homeIntent);*/
                // Log.d("main", "Free Memory: " + getUsedMemorySize());
                prefs = PreferenceManager
                        .getDefaultSharedPreferences(getApplicationContext());
    
    
    
            }
    
        }, 500)
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("main", "Ondestroy");
        // lock.reenableKeyguard();
        if (!call_flag && wm2 != null) {
            if (receive_flag) {
                if (isaddView) {
                    wm2.removeView(myview);
                }
    
            } else {
                wm2.removeView(new_view);
            }
    
            edit.putBoolean("flag", false);
            edit.commit();
            call_flag = true;
            if (CheckMissCall.bmp != null) {
                CheckMissCall.bmp.recycle();
                CheckMissCall.bmp = null;
    
    
            }
    
        }
    
    }
    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
    
        case R.id.slidehandlebutton:
             if (p != null)
                   showPopup(Unlock_hud.this, p);
            break;
    
    
        }
    }
    
    public void onWindowFocusChanged(boolean hasFocus) {
    
           int[] location = new int[2];
           Button button = (Button)myview. findViewById(R.id.slidehandlebutton);
    
           // Get the x, y location and store it in the location[] array
           // location[0] = x, location[1] = y.
           button.getLocationOnScreen(location);
    
           //Initialize the Point with x, and y positions
           p = new Point();
           p.x = location[0];
           p.y = location[1];
        }
    
    private void showPopup(Context context, Point p2) {
        // TODO Auto-generated method stub
        int popupWidth = 180;
           int popupHeight = 80;
    
           // Inflate the popup_layout.xml
           LinearLayout viewGroup = (LinearLayout) ((Activity) context).findViewById(R.id.popup);
           LayoutInflater layoutInflater = (LayoutInflater) context
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
    
           // Creating the PopupWindow
           final PopupWindow popup = new PopupWindow(c);
           popup.setWidth(popupWidth);
           popup.setHeight(popupHeight);
           popup.setFocusable(true);
    
           // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
           int OFFSET_X =-200;
           int OFFSET_Y =20;
    
           // Clear the default translucent background
           popup.setBackgroundDrawable(new BitmapDrawable());
    
           // Displaying the popup at the specified location, + offsets.
           popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
    
           // Getting a reference to Close button, and close the popup when clicked.
           Button close = (Button) layout.findViewById(R.id.stop);
           close.setOnClickListener(new OnClickListener() {
    
             @Override
             public void onClick(View v) {
               popup.dismiss();
             }
           });
        }
    

    现在我在(上下文)中发现了错误

     LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
           LayoutInflater layoutInflater = (LayoutInflater) context
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   Kaushik    10 年前

    我使用了你的代码。但似乎没问题。

    的完整代码段 Activity

    public class TestPopupActivity extends Activity implements OnClickListener {
    
        //The "x" and "y" position of the "Show Button" on screen.
        Point p;
        private LinearLayout holder;
        private LayoutInflater inflater;
        private View myView;
        private Button btn_show;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
    
           holder  = (LinearLayout) findViewById(R.id.holder);
           inflater = (LayoutInflater) getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
    
           myView = inflater.inflate(R.layout.inflated_layout, null);
           btn_show = (Button) myView.findViewById(R.id.show_popup);
           btn_show.setOnClickListener(this);
           holder.addView(myView);
        }
    
        // Get the x and y position after the button is draw on screen
        // (It's important to note that we can't get the position in the onCreate(),
        // because at that stage most probably the view isn't drawn yet, so it will return (0, 0))
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
    
           int[] location = new int[2];
          /* Button button = (Button) myView.findViewById(R.id.show_popup);*/
    
           // Get the x, y location and store it in the location[] array
           // location[0] = x, location[1] = y.
           btn_show.getLocationOnScreen(location);
    
           //Initialize the Point with x, and y positions
           p = new Point();
           p.x = location[0];
           p.y = location[1];
        }
    
        // The method that displays the popup.
        private void showPopup(Activity context, Point p) {
           int popupWidth = 200;
           int popupHeight = 150;
    
           // Inflate the popup_layout.xml
           LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
           LayoutInflater layoutInflater = (LayoutInflater) context
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
    
           // Creating the PopupWindow
           final PopupWindow popup = new PopupWindow(context);
           popup.setContentView(layout);
           popup.setWidth(popupWidth);
           popup.setHeight(popupHeight);
           popup.setFocusable(true);
    
           // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
           int OFFSET_X = 30;
           int OFFSET_Y = 30;
    
           // Clear the default translucent background
           popup.setBackgroundDrawable(new BitmapDrawable());
    
           // Displaying the popup at the specified location, + offsets.
           popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
    
           // Getting a reference to Close button, and close the popup when clicked.
           Button close = (Button) layout.findViewById(R.id.close);
           close.setOnClickListener(new OnClickListener() {
    
             @Override
             public void onClick(View v) {
               popup.dismiss();
             }
           });
        }
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.show_popup:
                if (p != null)
                       showPopup(TestPopupActivity.this, p);
                break;
    
            default:
                break;
            }
        }
        }
    

    截图

    enter image description here