Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓:处理器根本不工作_Java_Android_Handler - Fatal编程技术网

Java 安卓:处理器根本不工作

Java 安卓:处理器根本不工作,java,android,handler,Java,Android,Handler,我正在尝试运行一个处理程序,该处理程序每秒钟都会更改按钮上的文本,但它根本没有运行,我没有收到任何错误消息或什么都没有 这是密码 Button letterButton; Handler handler; String[] alphabet = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",

我正在尝试运行一个处理程序,该处理程序每秒钟都会更改按钮上的文本,但它根本没有运行,我没有收到任何错误消息或什么都没有

这是密码

Button letterButton;
Handler handler;

String[] alphabet = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
        "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
        "X", "Y", "Z" };

protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.changeletters);
    getSupportActionBar().hide();

    letterButton = (Button) findViewById(R.id.letterButton);
    letterButton.setOnClickListener(this);

    handler = new Handler();
    handler.postDelayed(run, 1000);
}


Runnable run = new Runnable() {

    @Override
    public void run() {
        int index = 0;
        letterButton.setText(alphabet[index++]);
    }
};

每次将
索引定义为
0
,然后将其递增,但在调用之后。 这就是为什么你的按钮总是显示“A”

移动
int索引=0handler=newhandler()之前,将code>编码到
onCreate


看起来有点过于复杂了,但经过一些训练后,它变得简单了。

我有一些简单的方法来实现它,无论我从ur req得到什么,它可能会在Z之后重复,所以我在旧代码中添加了一些调整

@Override
public void onCreate(Bundle bundle){

    // Your previous code...

    createRunnableWithIndex(0);

}

public void createRunnableWithIndex(final int index){

    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            letterButton.setText(alphabet[index]);
            createRunnableWithIndex((index+1) % 26);
        }

    }, 1000);

}
enter code here
Button letterButton;
 Handler handler;
int index = -1;
String[] alphabet = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
    "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
    "X", "Y", "Z" };
 protected void onCreate(Bundle bundle) {
 super.onCreate(bundle);
 setContentView(R.layout.changeletters);
 getSupportActionBar().hide();
 letterButton = (Button) findViewById(R.id.letterButton);
 letterButton.setOnClickListener(this);
 handler = new Handler();
 handler.postDelayed(run, 1000);
 }
 Runnable run = new Runnable() {
@Override
public void run() {
    if(index<alphabet.size()){
  index+=1;
   }else{
  index=0;
     }
    letterButton.setText(alphabet[index]);
 }
 };
在此处输入代码
按钮字母按钮;
处理者;
int指数=-1;
字符串[]字母={“A”、“B”、“C”、“D”、“E”、“F”、“G”、“H”、“I”、“J”,
“K”,“L”,“M”,“N”,“O”,“P”,“Q”,“R”,“S”,“T”,“U”,“V”,“W”,
“X”、“Y”、“Z”};
创建时受保护的void(捆绑){
super.onCreate(bundle);
setContentView(R.layout.changeletters);
getSupportActionBar().hide();
letterButton=(按钮)findViewById(R.id.letterButton);
letterButton.setOnClickListener(此);
handler=新的handler();
handler.postDelayed(运行,1000);
}
Runnable run=new Runnable(){
@凌驾
公开募捐{

如果(indexy)你需要每秒更新按钮文本?然后查看
计时器
类:我现在正在查看它,但我遇到了一个错误,你能给我一个使用它的示例吗?也许我使用它是错误的,我得到的错误是“只有创建视图层次结构的原始线程才能接触它的视图”@user3439273我上面提供的代码至少可以工作26秒:)谢谢。完美工作。第二个CreateRunnableHindex是否改为CreateRunnableHindex?是的。感谢您捕捉到这一点。如果您想通过使用处理程序重复更新该按钮,则处理程序的runnable必须采取步骤才能实现。谢谢,您的代码可以工作,但它会不断重复运行,另一个用户为我提供了一个我最喜欢的代码,并在我想要的时候完全停止,但需要做一点工作,这也可以使用。我不知道您是否希望它在Z处停止,或者是否希望它重复,但两者都很容易做到。唯一危险的事情是试图离开数组的末尾。当然,计时器也很好。这是因为y我们的索引始终为1…将索引变量移出一个字段。。。
enter code here
Button letterButton;
 Handler handler;
int index = -1;
String[] alphabet = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
    "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
    "X", "Y", "Z" };
 protected void onCreate(Bundle bundle) {
 super.onCreate(bundle);
 setContentView(R.layout.changeletters);
 getSupportActionBar().hide();
 letterButton = (Button) findViewById(R.id.letterButton);
 letterButton.setOnClickListener(this);
 handler = new Handler();
 handler.postDelayed(run, 1000);
 }
 Runnable run = new Runnable() {
@Override
public void run() {
    if(index<alphabet.size()){
  index+=1;
   }else{
  index=0;
     }
    letterButton.setText(alphabet[index]);
 }
 };