2016-09-21 2 views
1

Ich muss die Farbe eines leeren ImageView mit den Hex-Farbcode Werte im String Array transmitArray mit der Verzögerung in TransmitFreq angegeben ändern. Wenn ich jedoch den Code ausführe, wird nur die erste Farbe (entsprechend dem ersten Array-Wert) angezeigt.Java: Ändern Hintergrundfarbe mit einer Verzögerung mit Hex-Werten in einem Array

Ich habe versucht, drei Methoden, nämlich (Thread.Schlaf), Countdown-Timer und post.delayed, aber ohne Erfolg. Ich würde mich freuen, wenn jemand darauf hinweisen könnte, was ich falsch mache.

public class Main2Activity extends AppCompatActivity { 

    String [] transmitArray; 
    long transmitFreq; 
    public static int i; 
    public static View colourView; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.content_main2); 
     final String transmitArray [] = getIntent().getStringArrayExtra("COLOUR_DATA"); 
     transmitFreq = getIntent().getLongExtra("FREQ_VALUE", 0); 
     int arrayLength = transmitArray.length; 

     colourView = findViewById(R.id.colourBox); 

     /* 
     //Method 1: Using Countdown timer 

     new CountDownTimer(transmitFreq*(transmitArray.length), transmitFreq) { 

      public void onTick(long millisUntilFinished) { 
       colourView.setBackgroundColor(Color.parseColor(transmitArray[i])); 
       i++; 
      } 

      public void onFinish() { 
       i=0; 
      } 
     }.start(); 

     //Method 2: Using post.delayed 

     Handler handler = new Handler(); 
     for (i = 0; i < arrayLength ; i++) { 
      handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        String transmitColour = transmitArray[i]; 
        colourView.setBackgroundColor(Color.parseColor(transmitColour)); 
       } 
      }, transmitFreq); 
     }*/ 

     //Method 3: Using thread.sleep 

     for (i = 0; i < arrayLength ; i++) { 
      String transmitColour = transmitArray[i]; 
      colourView.setBackgroundColor(Color.parseColor(transmitColour)); 
      try { 
       Thread.sleep(transmitFreq);     
      } catch(InterruptedException ex) { 
       Thread.currentThread().interrupt(); 
      } 
     } 

    } 
} 

Antwort

0

Sie können zunächst eine Standardfarbe in der onCreate Methode verwenden und dann versuchen, die drei Methoden außerhalb der OnCreate-Methode. Versuchen Sie diesen Code

public class Main2Activity extends AppCompatActivity { 

String [] transmitArray; 
long transmitFreq; 
public static int i; 
public static View colourView; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_main2); 

    colourView = findViewById(R.id.colourBox); 
} 

final String transmitArray [] = getIntent().getStringArrayExtra("COLOUR_DATA"); 
    transmitFreq = getIntent().getLongExtra("FREQ_VALUE", 0); 
    int arrayLength = transmitArray.length; 


/* 
    //Method 1: Using Countdown timer 

    new CountDownTimer(transmitFreq*(transmitArray.length), transmitFreq) { 

     public void onTick(long millisUntilFinished) { 
      colourView.setBackgroundColor(Color.parseColor(transmitArray[i])); 
      i++; 
     } 

     public void onFinish() { 
      i=0; 
     } 
    }.start(); 

    //Method 2: Using post.delayed 

    Handler handler = new Handler(); 
    for (i = 0; i < arrayLength ; i++) { 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       String transmitColour = transmitArray[i]; 
       colourView.setBackgroundColor(Color.parseColor(transmitColour)); 
      } 
     }, transmitFreq); 
    }*/ 

    //Method 3: Using thread.sleep 

    for (i = 0; i < arrayLength ; i++) { 
     String transmitColour = transmitArray[i]; 
     colourView.setBackgroundColor(Color.parseColor(transmitColour)); 
     try { 
      Thread.sleep(transmitFreq);     
     } catch(InterruptedException ex) { 
      Thread.currentThread().interrupt(); 
     } 
    } 
} 
+0

Scheint nicht zu funktionieren. Ich bekomme den folgenden Fehler und einen App-Absturz. java.lang.NullPointerException: Versuch, die virtuelle Methode 'android.view.Window $ Callback android.view.Window.getCallback()' für eine Nullobjekt-Referenz aufzurufen –

Verwandte Themen