I need some help on android.., apparently, I’m trying to make a progress dialog for learning purposes, basically this progress dialog doesn’t do anything but shows the progress bar… I have a button in my main activity, upon clicking on that button, its onClickListener will run showDialog(1) method, which will call the main’s onCreateDialog method, however I’ve placed the button’s View.onClickListener as a separate class which is ButtonOnClickListener, all the necessary variables in the main activity are referenced in it, when I ran the activity in the avd emulator..when I click the button, it does not execute the progress dialog, but throws me an error “Sorry! the application has stopped unexpectedly. Please try again.” which forces me to quit… problem is with the ButtonOnClickListener class..which I still couldn’t debug for hours..
It only works if the the onClickListener is an anonymous class… some help?
This is the main activity class..
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 Class…
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 reported a null pointer exception at ButtonOnClickListener class at line 27…
Invokation of
onCreateDialogwill only assign values to the fields inDialogImproved. It will not assign values to the fields inButtonOnClickListenerI don’t know why you have dialog references in the
DialogImproved, and I don’t know what you want to do, but it actually looks like you instead want to use AsyncTask. The task should aggregate the dialog, theDialogImproveddoesn’t need to know about the progress dialog.