SetText在for循环中不起作用

时间:2018-07-07 13:28:05

标签: android

注意:我在Google和这里搜索了几个小时,但没有得到答案。

我想向值显示for loop返回的值。这是我的代码:

mProgressBar = findViewById(R.id.progressBar);
        percentText = findViewById(R.id.percent);

        mProgressBar.setMax(100);
        mProgressBar.setProgress(0);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    for (int progress = 0; progress<100; progress++){
                        mProgressBar.setProgress(progress);
                        percentText.setText(progress);
                        sleep(30);
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    Intent intent = new Intent(SplashScreenActivity.this, SignUpLoginActivity.class);
                    startActivity(intent);
                }

我添加此行percentText.setText(progress);时应用崩溃。没有这一行,代码将运行良好。我应该怎么做才能向TextView显示进度值?

7 个答案:

答案 0 :(得分:1)

好,因此,将我的意见添加到各种答案中提出的所有问题中,这是一个完整的有效示例活动。确定的主要问题是1)调用setText(int)而不是setText(String),以及2)在UI线程之外更新UI元素。

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private final Handler handler = new Handler();
    private TextView percentText;
    private ProgressBar mProgressBar;
    private int progress = 0;
    private final int updateFreqMs = 100; // call update every 100 ms

    @Override
    protected void onPause() {
        super.onPause();
        handler.removeCallbacksAndMessages(null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // reset the counter every time the activity resumes, whether 
        // you do this depends how you want it to behave
        progress = 0; 
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                updateProgress();
                if( progress < 100 ) {
                    handler.postDelayed(this, updateFreqMs);
                }
                else {
                    // Didn't test this part, but this is where you'd launch
                    // the next activity after the progress is complete
                    Intent intent = new Intent(MainActivity.this, SignUpLoginActivity.class);
                    startActivity(intent);

                    // probably also want to finish this so the user can't
                    // press back and come back here
                    finish();
                }
            }
        }, updateFreqMs);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_your_layout); // Put your layout id here
        percentText = findViewById(R.id.percent);
        mProgressBar = findViewById(R.id.progressBar);

        mProgressBar.setMax(100);
        mProgressBar.setProgress(0);
    }

    private void updateProgress() {
        // this will be called until progress == 100
        ++progress;
        mProgressBar.setProgress(progress);
        percentText.setText(String.valueOf(progress));
    }
}

答案 1 :(得分:0)

您似乎没有启动线程...

尝试

new Thread(new Runnable() {...all your code...})).start()

答案 2 :(得分:0)

您应该使用处理程序将for循环中的textview设置为进度

// Update the progress bar and display the
                //current value in the text view
                handler.post(new Runnable() {
                    public void run() {
                        progressBar.setProgress(progress);
                        textView.setText(progress);
                    }
                });

答案 3 :(得分:0)

您必须在UI线程而不是其他线程上更新UI。使用类似这样的代码

Handler mainHandler = new Handler(Looper.getMainLooper());

Runnable myRunnable = new Runnable() {
    @Override 
    public void run() {
         percentText.setText(progress);
    } 
};
mainHandler.post(myRunnable);

答案 4 :(得分:0)

您应该在UI线程中运行UI修改代码。

U可以像这样访问线程内部的UI线程:

new Thread(new Runnable() {
            @Override
            public void run() {
                yourActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                       percentText.setText(progress);
                    }
                });
            }

答案 5 :(得分:0)

  

当我添加这一行percentText.setText(progress);时,应用程序崩溃了;

只有UI线程可以访问视图。您无法从后台线程访问任何视图。使用Handler将任务发送到UI线程。

Handler mHandler = new Handler(Looper.getMainLooper());

 .............................

for (int progress = 0; progress<100; progress++){
   // mProgressBar.setProgress(progress);
   // percentText.setText(progress);
    mHandler.post(new Runnable() {
     @Override
     public void run() {
       // this will run in the main thread
       percentText.setText(progress);
       mProgressBar.setProgress(progress);
     }
     });
    sleep(30);

}

答案 6 :(得分:0)

这是因为进度是整数类型。代替

progressText.setText(progress);

这可能会导致找不到资源异常。

使用

progressText.setText(String.valueOf(progress));