我在android上需要一些帮助。.,显然,我正在尝试创建一个进度对话框以供学习,基本上,此进度对话框不执行任何操作,但显示进度条...我的主要活动中有一个按钮,单击该按钮后,其onClickListener将运行showDialog(1)方法,该方法将调用主体的onCreateDialog方法,但是我将按钮的View.onClickListener放置为一个单独的类,即ButtonOnClickListener,它是主活动中的所有必需变量当我在avd模拟器中运行该活动时,..单击该按钮时,它不会执行进度对话框,但会向我抛出错误“对不起!该应用程序意外停止。请重试。”这迫使我退出...问题出在ButtonOnClickListener类上。我仍然无法调试数小时。
仅当onClickListener是匿名类时才有效...有帮助吗?
这是主要的活动类别。
package edu.net.learn.android;
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
public class DialogImproved extends Activity{
CharSequence[] items = {"Google", "Apple", "Microsoft"};
boolean[] itemsChecked = new boolean[items.length];
private ProgressDialog _progressDialog;
private int _progress = 0;
private Handler _progressHandler;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
Button btn = (Button)this.findViewById(R.id.btn_dialog);
_progressHandler = new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
if(_progress >= 100){
_progressDialog.dismiss();
}//end if
else {
_progress++;
_progressDialog.incrementProgressBy(1);
_progressHandler.sendEmptyMessageDelayed(0, 100);
}
}
};
btn.setOnClickListener(new ButtonOnClickListener(this,_progress,_progressDialog,
_progressHandler));
}//end onCreate
protected Dialog onCreateDialog(int id){
switch(id){
case 1:
_progressDialog = new ProgressDialog(this);
_progressDialog.setIcon(R.drawable.icon);
_progressDialog.setTitle("Downloading files..");
_progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
_progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Hide", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "Hide clicked", Toast.LENGTH_SHORT).show();
}
});
_progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show();
}
});
return _progressDialog;
}
return null;
}
ButtonOnClickListener类别...
package edu.net.learn.android;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.view.View;
public class ButtonOnClickListener implements View.OnClickListener {
private Activity main;
private ProgressDialog _progressDialog;
private int _progress = 0;
private Handler _progressHandler;
ButtonOnClickListener(Activity main,int _progress,
ProgressDialog _progressDialog, Handler _progressHandler){
this.main = main;
this._progress = _progress;
this._progressDialog = _progressDialog;
this._progressHandler = _progressHandler;
}
public void onClick(View v){
_progress = 0;
_progressDialog.setProgress(0);
_progressHandler.sendEmptyMessage(0);
main.showDialog(1);
_progressHandler = new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
if(_progress >= 100){
_progressDialog.dismiss();
}//end if
else {
_progress++;
_progressDialog.incrementProgressBy(1);
_progressHandler.sendEmptyMessageDelayed(0, 100);
}
}
};
}
}
LogCat在第27行的ButtonOnClickListener类上报告了空指针异常...
最佳答案
调用onCreateDialog
仅会将值分配给DialogImproved
中的字段。不会为ButtonOnClickListener
中的字段分配值
我不知道为什么DialogImproved
中有对话框引用,我也不知道要做什么,但实际上看起来像是您想使用AsyncTask。该任务应聚合对话框,DialogImproved
无需了解进度对话框。