私はアンドロイドスタジオを使用してゲームをやっています。私は今日のゲームの読み込み画面のような何かをしたい、画面上にランダムなテキストが表示されます。画面上のランダムなテキストビューAndroid
例えば「Chargin one」「Chargin two」「Chargin three」と「Chargin four」のTextView 4文章を入れたいと思っています。アイデアは1つ表示し、1〜2秒待ってから次の他のものと続けることが示されている。
私はスレッドを実行してTextViewのテキストを設定してみましたが、次のエラーが発生しました。TextViewを変更できる唯一の缶はメインスレッドです。私は何をすべきか?
これは、スレッドにすることになっている:誰もがそれを必要とする場合、私はクラスを掲載する予定
int num = 1;
int aleatorio;
while(num<4) {
Random r = new Random();
aleatorio = r.nextInt(frasesCarga.size() - 1);
fraseCarga.setText(frasesCarga.get(aleatorio));
num++;
frasesCarga.remove(aleatorio);
}
。
LoadActivity.class
package es.fingerlabs.gamecohol;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Random;
public class Jugar extends AppCompatActivity {
private ArrayList<String> frasesCarga;
private TextView fraseCarga;
private int vivo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jugar);
fraseCarga = (TextView) findViewById(R.id.tvFraseCargandoJugar);
frasesCarga = new ArrayList<String>();
frasesCarga.add("Preparando Cubatas");
frasesCarga.add("Haciendo hielo");
frasesCarga.add("Mezclando preguntas");
frasesCarga.add("Cogiendo sitio");
frasesCarga.add("Poniendo chupitos");
Thread timerThread = new Thread() {
public void run() {
try {
int num = 1;
int aleatorio;
while(num<4) {
sleep(1000);
Random r = new Random();
aleatorio = r.nextInt(frasesCarga.size() - 1);
fraseCarga.setText(frasesCarga.get(aleatorio));
num++;
frasesCarga.remove(aleatorio);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
timerThread.start();
}
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("¿Salir de la partida?")
.setMessage("Si sales se perdera el progreso de la partida")
.setPositiveButton("Si", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
しかし、あなたはonCreate()にそのコードを書いています。あなたが話している他のスレッドはどこですか? –
投稿を編集してスレッドを追加して理解しやすくしました –