对话框未显示在活动组中

时间:2013-05-15 10:59:25

标签: android dialog radio-button

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

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


}

3 个答案:

答案 0 :(得分:0)

Activity activity = getActivty.getParent();

if (activity != null)
  Dialog dialog=new Dialog(activity);

答案 1 :(得分:0)

可能的问题可能是您的广播组onclick侦听器中的v.getContext()。请尝试其中任何一项而不是getBaseContext()getApplicationContext(),或尝试传递context对象。

答案 2 :(得分:0)

尝试使用:

Dialog dialog = new Dialog(getParent());