我的问题: 我实际上想要随机设置我的ImageView的可见性。我的意思是每个图像视图将在几毫秒后随机可见,但我的应用程序意外地一直崩溃。虽然,我是Android开发新手,但擅长Java J2SE。请告诉我我在做什么是偶然的错误或错误?请!提前谢谢!
package com.example.app;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button b;
public ImageView I1;
public ImageView I2;
public ImageView I3;
public ImageView I4;
public TextView T;
public TextView s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
I1=new ImageView(this);
I1=(ImageView) findViewById(R.id.imag1);
I1.setVisibility(View.INVISIBLE);
I2=new ImageView(this);
I2=(ImageView) findViewById(R.id.imag2);
I2.setVisibility(View.INVISIBLE);
I3=new ImageView(this);
I3=(ImageView) findViewById(R.id.imag3);
I3.setVisibility(View.INVISIBLE);
I4=new ImageView(this);
I4=(ImageView) findViewById(R.id.imag4);
I4.setVisibility(View.INVISIBLE);
T=(TextView)findViewById(R.id.time);
s=(TextView)findViewById(R.id.score);
Thread t=new Thread(new MyThread());
t.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class MyThread implements Runnable{
Random randomGenerator = new Random();
int n;
public void run(){
while(true){
n=randomGenerator.nextInt(8);
if(n==1){
I1.setVisibility(View.VISIBLE);
}
if(n==2){
I2.setVisibility(View.VISIBLE);
}
if(n==3){
I3.setVisibility(View.VISIBLE);
}
if(n==4){
I4.setVisibility(View.VISIBLE);
}
try {
Thread.currentThread().sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.getStackTrace();
}
}
}
}
}
编辑代码:
public ImageView I1;
public ImageView I2;
public ImageView I3;
public ImageView I4;
public TextView T;
public TextView s;
Random randomGenerator = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
I1=new ImageView(this);
I1=(ImageView) findViewById(R.id.imag1);
I1.setVisibility(View.INVISIBLE);
I2=new ImageView(this);
I2=(ImageView) findViewById(R.id.imag2);
I2.setVisibility(View.INVISIBLE);
I3=new ImageView(this);
I3=(ImageView) findViewById(R.id.imag3);
I3.setVisibility(View.INVISIBLE);
I4=new ImageView(this);
I4=(ImageView) findViewById(R.id.imag4);
I4.setVisibility(View.INVISIBLE);
T=(TextView)findViewById(R.id.time);
s=(TextView)findViewById(R.id.score);
runOnUiThread(new Runnable() //run on ui thread
{
int n;
public void run(){
while(true){
n=randomGenerator.nextInt(4)+1;
if(n==1){
I1.setVisibility(View.VISIBLE);
}
if(n==2){
I2.setVisibility(View.VISIBLE);
}
if(n==3){
I3.setVisibility(View.VISIBLE);
}
if(n==4){
I4.setVisibility(View.VISIBLE);
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
答案 0 :(得分:1)
android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。
您必须如上所述获得例外。您需要在UI线程上更新ui。您正在更新线程中的UI。因此异常和崩溃。
在你的线程中使用runOnUiThread来更新UI
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
while(true){
n=randomGenerator.nextInt(8);
if(n==1){
I1.setVisibility(View.VISIBLE);
}
....
}
}
});
同时删除此I1 = new ImageView(this);因为你正在初始化为I1 =(ImageView)findViewById(R.id.imag1);
编辑:
我建议使用Handler来实现此目的
int n;
Handler m_handler;
Runnable m_handlerTask ;
Random r;
将上述声明为类变量;
在活动onCrate()内部使用如下处理程序
r= new Random();
m_handler= new Handler();
m_handlerTask = new Runnable()
{
@Override
public void run() {
n=r.nextInt(4)+1;
if(n==1){
I1.setVisibility(View.VISIBLE);
I2.setVisibility(View.INVISIBLE);
I3.setVisibility(View.INVISIBLE);
I4.setVisibility(View.INVISIBLE);
}
if(n==2){
I2.setVisibility(View.VISIBLE);
I1.setVisibility(View.INVISIBLE);
I3.setVisibility(View.INVISIBLE);
I4.setVisibility(View.INVISIBLE);
}
if(n==3){
I3.setVisibility(View.VISIBLE);
I1.setVisibility(View.INVISIBLE);
I2.setVisibility(View.INVISIBLE);
I4.setVisibility(View.INVISIBLE);
}
if(n==4){
I4.setVisibility(View.VISIBLE);
I1.setVisibility(View.INVISIBLE);
I3.setVisibility(View.INVISIBLE);
I2.setVisibility(View.INVISIBLE);
}
m_handler.postDelayed(m_handlerTask, 50);
// 50 milli seconds. set this to desired number
}
};
m_handlerTask.run();
请记住在不需要时停止处理程序
m_handler.removeCallbacks(m_handlerTask);
答案 1 :(得分:1)
我想你想要完整的代码。你可以使用计时器。请参阅以下代码。
public class MainActivity extends Activity {
public ImageView I1;
public ImageView I2;
public ImageView I3;
public ImageView I4;
public TextView T;
public TextView s;
int n;
Timer t;
TimerTask task;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
I1 = (ImageView) findViewById(R.id.imag1);
I1.setVisibility(View.INVISIBLE);
I2 = (ImageView) findViewById(R.id.imag2);
I2.setVisibility(View.INVISIBLE);
I3 = (ImageView) findViewById(R.id.imag3);
I3.setVisibility(View.INVISIBLE);
I4 = (ImageView) findViewById(R.id.imag4);
I4.setVisibility(View.INVISIBLE);
T = (TextView) findViewById(R.id.time);
s = (TextView) findViewById(R.id.score);
startTimer();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void startTimer() {
//Random randomGenerator = new Random();
//n=randomGenerator.nextInt(8);
final Random randomGenerator = new Random();
t = new Timer();
task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
//n++;
n=randomGenerator.nextInt(8);
if (n == 1) {
I1.setVisibility(View.VISIBLE);
I2.setVisibility(View.INVISIBLE);
I3.setVisibility(View.INVISIBLE);
I4.setVisibility(View.INVISIBLE);
}
if (n == 2) {
I1.setVisibility(View.INVISIBLE);
I2.setVisibility(View.VISIBLE);
I3.setVisibility(View.INVISIBLE);
I4.setVisibility(View.INVISIBLE);
}
if (n == 3) {
I1.setVisibility(View.INVISIBLE);
I2.setVisibility(View.INVISIBLE);
I3.setVisibility(View.VISIBLE);
I4.setVisibility(View.INVISIBLE);
}
if (n == 4) {
I1.setVisibility(View.INVISIBLE);
I2.setVisibility(View.INVISIBLE);
I3.setVisibility(View.INVISIBLE);
I4.setVisibility(View.VISIBLE);
}
}
});
}
};
t.scheduleAtFixedRate(task, 0, 1000);
}
}
这将完美地运作。我希望这会对你有所帮助。
答案 2 :(得分:0)
很完美! 100%
package com.example.abc;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Test extends Activity {
public Button B;
public Button B1;
public TextView S;
int count=0;
Random randomGenerator = new Random();
int n;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
B=new Button(this);
B=(Button)findViewById(R.id.button1);
B.setVisibility(View.INVISIBLE);
B1=new Button(this);
B1=(Button)findViewById(R.id.button2);
B1.setVisibility(View.INVISIBLE);
S=(TextView)findViewById(R.id.score);
B.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
count+=1;
S.setText(Integer.toString(count));
}
});
B.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
count+=1;
S.setText(Integer.toString(count));
}
});
runThread();
}
private void runThread() {
new Thread() {
public void run() {
while ( i++ < 1200) {
if(i==1200){
startActivity(new Intent(Test.this,Game.class));
break;
}
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
n = randomGenerator.nextInt(4)+1;
if(n==2){
B.setVisibility(View.VISIBLE);
B1.setVisibility(View.INVISIBLE);
}
if(n==4){
B.setVisibility(View.INVISIBLE);
B1.setVisibility(View.VISIBLE);
}
}
});
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test, menu);
return true;
}
}