1
TransmitFreqで指定された遅延を使用して、文字列配列のtransmitArrayに格納された16進カラーコード値を使用して空白のImageViewの色を変更する必要があります。しかし、私がコードを実行すると、(最初の配列値に対応する)最初の色だけが表示されます。Java:配列内の16進値を使用して背景色を変更する
私は、(thread.sleep)、カウントダウンタイマーとpost.delayedという3つの方法を試みましたが、成功しませんでした。誰かが私が間違っていることを指摘できたら、私は感謝します。
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();
}
}
}
}
動作していないようです。私は次のエラーとアプリケーションのクラッシュを取得します。 java.lang.NullPointerException:ヌルオブジェクト参照に仮想メソッド 'android.view.Window $ Callback android.view.Window.getCallback()'を呼び出そうとしています –