문제

I'm trying to setMax value of progress bar, and setProgress to it, but caught NullPointerException. I use AsyncTask to update values and Dialog with custom layout, which contains ProgressBar. So here's my code. Point please to my mistakes. Thanks

public class ActivityMain extends Activity implements View.OnClickListener {

final int PROGRESS_DLG_ID = 505;
ProgressBar pb = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_main);

    Button button_db = (Button) findViewById(R.id.button_db);
    Button button_settings = (Button) findViewById(R.id.button_settings);
    Button button_exit = (Button) findViewById(R.id.button_exit);
    pb = (ProgressBar) findViewById(R.id.db_download_pb);
    new DBLoad().execute("cards_en.db");

@Override
protected Dialog onCreateDialog(int dialogId) {
    Dialog progress = null;
    switch (dialogId) {
        case PROGRESS_DLG_ID:
            progress = new Dialog(this);
            progress.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            progress.setContentView(R.layout.db_download_dialog);
            break;
    }
    return progress;
}

And this is onProgressUpdate method where i have exception

@Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        showDialog(PROGRESS_DLG_ID);
        pb.setMax(values[1]); //here throws exception
        pb.setProgress(values[0]);
    }

SOLVED Add correct init of progress bar at

@Override
protected Dialog onCreateDialog(int dialogId) {
    Dialog progress = null;
    switch (dialogId) {
        case PROGRESS_DLG_ID:
            progress = new Dialog(this);
            progress.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            progress.setContentView(R.layout.db_download_dialog);
            pb = (ProgressBar) progress.findViewById(R.id.cards_download_bar);
            break;
    }
    return progress;
}

Also moved method setMax to preExecute().

도움이 되었습니까?

해결책

Why do you set max value every onProgressUpdate()? Wouldn't be easy if you set it once in onPreExecute()? Have you set your initial progress?

Please, paste your AsyncTask class to understand better.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top