今天在看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);