你好,游客 登录 注册 搜索
背景:
阅读新闻

Android ProgressDialog错误:Unable to add window

[日期:2011-11-13] 来源:Linux社区  作者:wanzdhan [字体:大 中 小]

今天在看API学习ProgressDialog时遇到的一个问题
      

Android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

package com.covics.zfh;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class HelloDialogActivity extends Activity {
 
 private Button btn_alert;
 private Button btn_progress;
 private Button btn_custom
 ;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        btn_alert = (Button)findViewById(R.id.btn_alertdialog);
        btn_progress = (Button)findViewById(R.id.btn_progressdialog);
        btn_custom = (Button)findViewById(R.id.btn_customdialog);
       
        btn_alert.setOnClickListener(new MyOnClickListener());
        btn_progress.setOnClickListener(new MyOnClickListener());
        btn_custom.setOnClickListener(new MyOnClickListener());
    }
   
    class MyOnClickListener implements OnClickListener {

  @Override
  public void onClick(View v) {
   switch (v.getId()) {
   case R.id.btn_alertdialog : {
//    System.out.println("btn_alertdialog");
    alertDialog();
    break;
   }
   case R.id.btn_progressdialog : {
    progressDialog(getApplicationContext());
    break;
   }
   case R.id.btn_customdialog : {
    ProgressDialog progress = ProgressDialog.show(getApplicationContext(),
      "", "loading...", true);
//    progress.show();
    break;
   }
   
   default :
    break;
   }
   
  }
    }

    private void alertDialog(){
     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setMessage("Are you sure to exit ?")
     .setCancelable(false)
     .setPositiveButton("yes", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    HelloDialogActivity.this.finish();
   }
  })
  .setNegativeButton("no", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    dialog.cancel();
   }
  });
     AlertDialog dialog = builder.create();
     dialog.show();
    }
    private void alertDialog2(final Context context){
     final CharSequence[] colors = {"red", "green", "yellow"};
     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setTitle("pick a color");
     builder.setItems(colors, new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    System.out.println("choose ---" + dialog.toString()+" "+which);
    Toast.makeText(context, dialog.toString()+" "+which, Toast.LENGTH_SHORT).show();
   }
  });
     AlertDialog dialog = builder.create();
     dialog.show();
    }
    private void progressDialog(final Context context){
     ProgressDialog progress;
     progress = new ProgressDialog(context);
     progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
     progress.setCancelable(false);
     progress.show();
    }
}

 

改正方法:将getApplicationContext()换成Activity.this.这样就可以了.

即:ProgressDialog progress = ProgressDialog.show(HelloDialogActivity.this,
      "", "loading...", true);

linux
相关资讯       Android基础教程  Android开发教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款