2017-05-01 11 views
0

Android開発の新機能です。以前の自動クリックボタンの投稿を確認しましたが、これらのエラーを設定することはできません。 アプリケーションは、5秒タイマーの後にを停止します。以下はAndroid用の自動クリックボタン

MainActivityに私のコードです:


package com.example.cynog.autobutton; 

import android.content.Intent; 
import android.content.res.Resources; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends AppCompatActivity { 

    private Button button1; 

    protected void onCreate(Bundle savedInstanceState) { 
     try { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      performClick(); 

      Thread timer = new Thread(){ 
       public void run(){ 
        try{ 
         sleep(5000); 
        } catch (InterruptedException e){ 
         e.printStackTrace(); 
        }finally{ 
         button1.performClick(); 
        } 
       } 
      }; 
      timer.start(); 
     } catch (Resources.NotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public void performClick() { 
     button1 = (Button) findViewById(R.id.button); 
     button1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(MainActivity.this, activity.class); 
       startActivity(i); 
      } 
     }); 
    } 
} 

そして、これは私のXMLです:


<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.cynog.autobutton.MainActivity"> 


    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     android:onClick="performClick" 
     tools:layout_editor_absoluteX="153dp" 
     tools:layout_editor_absoluteY="209dp" /> 
</android.support.constraint.ConstraintLayout> 

答えて

1

ボタンがメインで、UIスレッドで作成されます糸。 もしあなたがそれを使って何らかの行動をしたいのであれば、それはhandler.post(new Runnable(){void run(){//perform action here}の中で行います。 Handler object in oncreate(). Handler handler=new Handler(). を作成し、スリープ後にtimerスレッド実行メソッドにhandler.post()コードを挿入します。私はそれがうまくいきたい

+0

ありがとうございます! –

関連する問題