繁体   English   中英

如何使用android中的多线程更新android中的TextView

[英]How to update the TextView in android using Multithreading in android

public class MainActivity extends Activity implements Runnable{
final TextView txtCounter = (TextView)findViewById(R.id.counter);
Thread mythread;
int i = 0;
boolean running = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button btnstart= (Button)findViewById(R.id.btnStart);
    final Button btnstop = (Button)findViewById(R.id.btnStop);
    
    btnstart.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View arg0) {
            try{
                running = true;
                mythread = new Thread();
                mythread.start();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });
    
    btnstop.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View arg0) {
            try{
                running = false;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });
    
    
}



@Override
public void run(){
    try{
        while(i <= 1234567 && running){
            Thread.sleep(1000);
            i++;
            txtCounter.setText(i);
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
  }

  }

我正在尝试在按下开始按钮时使用两个按钮,它每秒递增数字并更新 TextView 直到按下停止按钮。 在 activity_main.xml 文件中只有一个 TextView 和两个按钮,一个用于启动另一个用于停止。

在我们按下停止按钮后,如果再次按下开始,它将从最后更新的最后一个数字开始。

但我收到一个错误

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.prg4/com.example.prg4.MainActivity}: java.lang.NullPointerException
public class MainActivity extends Activity implements Runnable{
final TextView txtCounter = (TextView)findViewById(R.id.counter);
Thread mythread;
int i = 0;
boolean running = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button btnstart= (Button)findViewById(R.id.btnStart);
    final Button btnstop = (Button)findViewById(R.id.btnStop);
    
    btnstart.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View arg0) {
            try{
                running = true;
                mythread = new Thread();
                mythread.start();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });
    
    btnstop.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View arg0) {
            try{
                running = false;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });
    
    
}



@Override
public void run(){
    try{
        while(i <= 1234567 && running){
            Thread.sleep(1000);
            i++;
            txtCounter.setText(i);
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
  }

  }

我试图在按下开始按钮时使用两个按钮,它每秒增加一次数字并更新 TextView 直到按下停止按钮。 在 activity_main.xml 文件中只有一个 TextView 和两个按钮,一个用于启动,另一个用于停止。

在我们按下停止按钮后,如果再次按下开始,它将从最后更新的最后一个数字开始。

但我收到一个错误

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.prg4/com.example.prg4.MainActivity}: java.lang.NullPointerException

我的回答可能不正确,因为我没有正确理解你。 也许,你可以从这个启动画面得到答案

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        textView.setText(charSequence.subSequence(0,index++));
        if(index <= charSequence.length()){
            handler.postDelayed(runnable,delay);
        }
    }
};

public void animatText(CharSequence cs){
    charSequence = cs;
    index = 0;
    textView.setText("");
    handler.removeCallbacks(runnable);
    handler.postDelayed(runnable,delay);
}

我将它添加到启动画面。 文本(二进制)将要运行

Output 就像:

白娜 r y

每个字母一一显示。 我不确定这是不是你的答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM