0

在我的应用程序中,我正在显示来自非活动类的自定义对话框。它可以工作,但很少崩溃,原因如下:

android.view.windowmanager Bad TokenException:unable to add window-
token android.os.binderproxy@2afac69d8 is not valid;is your activity running?

我的编码结构:

 MainActivity.java(Activity class)

 public class MainActivity extends TabActivity {
    @Override
     public void onCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
            ........
            UpdateClass obj = new UpdateClass(MainActivity.this);
            obj.checkUpdate(); 
      }
 }

UpdateClass.java(非活动类)

public class UpdateClass{

UpdateClass(MainActivity mainActivity ){
  this.context = mainActivity;
}

checkUpdate(){

    dialog_Update = new Dialog(context);
    ...
    updateButton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
        ....
        }
    });
    cancelButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            dialog_Update.dismiss();
        }
    });

    dialog_Update.show();  
   ......               
 } 
} 
4

1 回答 1

0

只需尝试如下实现:

public class MainActivity extends TabActivity {
@Override
 private Context context;
 public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
        ........
      context=MainActivity.this;
        UpdateClass obj = new UpdateClass(context);
        obj.checkUpdate(); 
  }

}

在您的UpdateClass. 而不是传递MainActivityin 构造函数传递Activity如下:

public class UpdateClass{
    pdateClass(Activity mainActivity ){ <----Pass the activity here.
    this.context = mainActivity;
  }
     ......               
  } 
 } 
于 2013-01-16T12:56:48.463 回答