Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem and couldn't separate a View.onClickListener's class with the main class..help!
    primarykey
    data
    text
    <p>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..</p> <p>It only works if the the onClickListener is an anonymous class... some help?</p> <p>This is the main activity class..</p> <pre><code>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 &gt;= 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; } </code></pre> <p>ButtonOnClickListener Class...</p> <pre><code>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 &gt;= 100){ _progressDialog.dismiss(); }//end if else { _progress++; _progressDialog.incrementProgressBy(1); _progressHandler.sendEmptyMessageDelayed(0, 100); } } }; } } </code></pre> <p>LogCat reported a null pointer exception at ButtonOnClickListener class at line 27...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload