-1

我正在尝试设置进度条的最大值,并将其设置为设置进度,但捕获了 NullPointerException。我使用 AsyncTask 来更新值和具有自定义布局的对话框,其中包含 ProgressBar。所以这是我的代码。请指出我的错误。谢谢

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;
}

这是我有异常的 onProgressUpdate 方法

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

已解决 在以下位置添加正确的进度条初始化

@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;
}

还将方法 setMax 移至 preExecute()。

4

1 回答 1

0

为什么每个 onProgressUpdate() 都设置最大值?如果在 onPreExecute() 中设置一次会不会很容易?你设置好你的初始进度了吗?

请粘贴您的 AsyncTask 类以更好地理解。

于 2013-10-13T00:12:27.287 回答