我试图在一段时间后将4张图像设置为不可见和可见,如蓝色图像显示,蓝色图像隐藏,黄色图像显示,黄色图像隐藏。我想这样做没有事件,只有for循环。我正在尝试以下但它不起作用。我是android和图形的新手。请帮帮我
public class GameActivity extends Activity {
/** Called when the activity is first created. */
private Handler mHandler = new Handler();
ImageView[] imViews = new ImageView[4];
ImageView imageView1;
ImageView imageView2;
ImageView imageView3;
ImageView imageView4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myviewlay);
imageView1= (ImageView)findViewById(R.id.ImageView01);
imageView2= (ImageView)findViewById(R.id.ImageView02);
imageView3= (ImageView)findViewById(R.id.ImageView03);
imageView4= (ImageView)findViewById(R.id.ImageView04);
final ImageView[] imViews = new ImageView[4];
imViews[0] = imageView1;
imViews[1] = imageView2;
imViews[2] = imageView3;
imViews[3] = imageView4;
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
for(int i=0; i < 5; i++)
{
imViews[i].setVisibility(View.INVISIBLE);
mHandler.postDelayed(this, 1000);
imViews[i].setVisibility(View.INVISIBLE);
mHandler.postDelayed(this, 1000);
}
}};
}
我的错误日志:
04-23 05:25:16.383: ERROR/AndroidRuntime(506): java.lang.NullPointerException
04-23 05:25:16.383: ERROR/AndroidRuntime(506): at com.games.game.GameActivity$1.run(GameActivity.java:60)
04-23 05:25:16.383: ERROR/AndroidRuntime(506): at android.os.Handler.handleCallback(Handler.java:587)
04-23 05:25:16.383: ERROR/AndroidRuntime(506): at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 05:25:16.383: ERROR/AndroidRuntime(506): at android.os.Looper.loop(Looper.java:123)
04-23 05:25:16.383: ERROR/AndroidRuntime(506): at android.app.ActivityThread.main(ActivityThread.java:3647)
04-23 05:25:16.383: ERROR/AndroidRuntime(506): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 05:25:16.383: ERROR/AndroidRuntime(506): at java.lang.reflect.Method.invoke(Method.java:507)
04-23 05:25:16.383: ERROR/AndroidRuntime(506): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-23 05:25:16.383: ERROR/AndroidRuntime(506): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-23 05:25:16.383: ERROR/AndroidRuntime(506): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
尝试使用:
imViews[i].setVisibility(View.GONE);
同样如@MByD所说,通过只调用一次来改进你的代码。
答案 1 :(得分:1)
你无法通过使用postDelayed()来实现这一点。该方法不会在循环中暂停您的方法。使用Thread.sleep()可以实现 。但是,在UI线程上执行此操作最终会导致强制关闭,因为您将阻止UI线程太长时间。
基本上你需要某种后台线程,等待你,然后将一个runnable发布到显示/隐藏视图的UI线程。
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
for(int i=0; i < 5; i++)
{
final ImageView view = imViews[i];
mHandler.post(new Runnable() {
public void run() {
view.setVisibility(View.INVISIBLE);
}
});
try {
Thread.sleep(1000);
} catch (Exception e) {}
mHandler.post(new Runnable() {
public void run() {
view.setVisibility(View.VISIBLE);
}
});
try {
Thread.sleep(1000);
} catch (Exception e) {}
}
}};
}
在onCreate()方法中,用:
启动它new Thread(mUpdateTimeTask).start();
您可能还想查看TimerTask课程,该课程可以帮助您定期运行内容。有几种方法可以做这些事情。