在ProgressBar

时间:2015-11-05 17:19:45

标签: android

我正在尝试在微调器进度条完成后更新按钮单击时TextView的文本。

public void launchRingDialog(View view) {

        final ProgressDialog ringProgressDialog = ProgressDialog.show(MainActivity.this, "Please wait ...", "Downloading File ...", true);
        ringProgressDialog.setCancelable(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String text;
                    int build_num = android.os.Build.VERSION.SDK_INT;
                    Set buildNumbers = getSharedPreferenceData(build_numbers);
                    boolean result = isPresent(buildNumbers,
                            build_num);
                    if (result) {
                        text = "Present";
                    } else {
                        text = "Not Present";
                    }
                    //Thread.sleep(5000);
                    System.out.println("Text After button click");
                    System.out.println(text);

                     TextView compliance = (TextView) findViewById(R.id.compliance);
                    compliance.setText(text);

                } catch (Exception e) {

                }
                ringProgressDialog.dismiss();
            }
        }).start();
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.download_file) {
            System.out.println("Inside button click");
            launchRingDialog(v);
       }
 }

在onCreate中,我检查内部版本号,并将文本设置为" Present"。但是当我按下按钮时,sharedPreference文件会被更改,我会检查当前的构建编号,它返回" Not Present,这不会反映在TextView中。

线程是否在这里造成问题?

1 个答案:

答案 0 :(得分:1)

问题在于您尝试从另一个线程调用compliance.setText(text)方法。 Android上不允许这样做。对于您尝试做的事情,您应该使用AsyncTask而不是启动自己的线程。像这样:

public void launchRingDialog(View view) {

        final ProgressDialog ringProgressDialog = ProgressDialog.show(MainActivity.this, "Please wait ...", "Downloading File ...", true);
        ringProgressDialog.setCancelable(true);

        new AsyncTask<Void,Void,String>(){

            @Override
            protected String doInBackground(Void... params) {

                String text;
                int build_num = android.os.Build.VERSION.SDK_INT;
                Set buildNumbers = getSharedPreferenceData(build_numbers);
                boolean result = isPresent(buildNumbers,
                        build_num);
                if (result) {
                    text = "Present";
                } else {
                    text = "Not Present";
                }

                return text;
            }

            @Override
            protected void onPostExecute(String s) {
                TextView compliance = (TextView) findViewById(R.id.compliance);
                compliance.setText(s);
                ringProgressDialog.dismiss();
            }
        }.execute();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.download_file) {
            System.out.println("Inside button click");
            launchRingDialog(v);
        }
    }