在Android上的两个弹出窗口之间共享数据

时间:2014-04-28 11:31:50

标签: android popup

我的应用上有一个popup,可以打开另一个而不关闭。问题是我想在第二个popup放置一些数据,关闭它,并在第一个上加载数据。这是我的代码:

popup.setOnDismissListener(new OnDismissListener() 
        { 
            public void onDismiss() 
            {
                TextView tv = (TextView)findViewById(R.id.tv_pagocontarjeta);
                tv.setText(((TextView)layout.findViewById(R.id.calc_tv)).getText());
            }
        });

但它不起作用,我在行上得到 NullPointerException

tv.setText(((TextView)layout.findViewById(R.id.calc_tv)).getText());

我该怎么办?感谢

3 个答案:

答案 0 :(得分:0)

而不是

TextView tv = (TextView)findViewById(R.id.tv_pagocontarjeta);

使用

TextView tv = (TextView)layout.findViewById(R.id.tv_pagocontarjeta);

答案 1 :(得分:0)

试着希望它有效:

popup.setOnDismissListener(new OnDismissListener() 
    { 
        public void onDismiss() 
        {
            TextView tv = (TextView)findViewById(R.id.tv_pagocontarjeta);
            tv.setText(((TextView)layout.findViewById(R.id.calc_tv)).getText().toString());
        }
    });

答案 2 :(得分:0)

我通过创建弹出窗口的静态变量来解决这个问题:

static View popup_forpag;
static View popup_buscadorgenerico; 

然后我从他们那里夸大了弹出窗口:

FrameLayout fl = (FrameLayout) findViewById(R.id.dim_bg);
    fl.setVisibility(View.VISIBLE);

    final Activity context = Activity_Mesas.this;

    // Inflate the popup_layout.xml
    LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.Buscador_Generico);
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    popup_buscadorgenerico = layoutInflater.inflate(R.layout.buscador_generico, viewGroup);

    // Creating the PopupWindow
    final PopupWindow popup = new PopupWindow(context);
    popup.setContentView(popup_buscadorgenerico);

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;

    popup.setWidth((int) (width*0.8));
    popup.setHeight((int) (height*0.8));
    popup.setFocusable(true);

    // Clear the default translucent background
    popup.setBackgroundDrawable(new BitmapDrawable());

    // Displaying the popup at the specified location, + offsets.
    popup.showAtLocation(popup_buscadorgenerico, Gravity.CENTER, 0, 0);

最后:

popup.setOnDismissListener(new OnDismissListener() 
    { 
        public void onDismiss() 
        {
            TextView tv = (TextView)popup_forpag.findViewById(R.id.tv_pagocontarjeta);
            tv.setText(((TextView)popup_buscadorgenerico.findViewById(R.id.calc_tv)).getText());
        }
    });

感谢您的回答。