0

我有一个 Tabactivity 我有 4 个标签。其中一个选项卡有许多通过 ActivityGroup 显示的子活动..我在其中一个子活动中有单选按钮..单击任何单选按钮时我需要显示一个对话框...m 无法显示该对话框框.....我收到如下错误:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@412369a0 is not valid; is your activity running?

我的代码在这里:

public class BabyProducts extends ActivityGroup {

Button back,home;

RadioGroup rg_babybath,
Context context=this;
String babybath;

RadioButton rb_babybath;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.babyproducts);

    rg_babybath=(RadioGroup)findViewById(R.id.radioGroup_babybath);

    back=(Button)findViewById(R.id.btn_back);
    home=(Button)findViewById(R.id.btn_home);
    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent activity2=new Intent(v.getContext(),Inventory.class);
            replaceContentView("activity2", activity2);

        }
    });

    home.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent activity2=new Intent(v.getContext(),AuditActivity.class);
            replaceContentView("activity2", activity2);

        }
    });

    rg_babybath.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            rb_babybath=(RadioButton)findViewById(checkedId);
            babybath=rb_babybath.getText().toString();
            option(babybath);


}

});
}

public void option(String bbath)
{
    if(bbath.equals("Yes")){
        final Dialog dialog=new Dialog(BabyProducts.this);
        dialog.setContentView(R.layout.popup_stock);
        dialog.setTitle("Choose");
        Button save=(Button)dialog.findViewById(R.id.save);
        Button submit=(Button)dialog.findViewById(R.id.submit);


        save.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                dialog.dismiss();
            }

        });

        submit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                dialog.dismiss();
            }
        });
        dialog.show();

    }

    if(bbath.equals("No")){
        final Dialog dialog=new Dialog(BabyProducts.this);
        dialog.setContentView(R.layout.popup_reason);
        dialog.setTitle("Choose");
        Button save=(Button)dialog.findViewById(R.id.save);
        Button submit=(Button)dialog.findViewById(R.id.submit);


        save.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                dialog.dismiss();
            }

        });

        submit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                dialog.dismiss();
            }
        });
        dialog.show();

    }


}

@SuppressWarnings("deprecation")
public void replaceContentView(String id, Intent newIntent)
{
    View view=getLocalActivityManager().startActivity(id, newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
    this.setContentView(view);
}


}
4

3 回答 3

0

The likely issue might be the v.getContext() in your radio group onclick listener. Try any of these instead of it getBaseContext() or getApplicationContext() or try passing your context object.

于 2013-05-15T11:22:00.207 回答
0
Activity activity = getActivty.getParent();

if (activity != null)
  Dialog dialog=new Dialog(activity);
于 2013-05-15T11:03:41.667 回答
0

尝试使用:

Dialog dialog = new Dialog(getParent());
于 2013-05-15T12:05:18.070 回答