我是android领域的新手。我正在努力定期在 main.xml 的中心显示两个图像。代码运行成功,但没有显示任何图像。这是代码。
package com.example.radio;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
public class Radio extends Activity
{
private ImageView _imagView;
private Timer _timer;
private int _index;
private MyHandler handler;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio);
handler= new MyHandler();
_imagView=(ImageView) findViewById(R.id.speak);
_index=0;
_timer= new Timer();
_timer.schedule(new TickClass(), 500, 200);
}
private class TickClass extends TimerTask
{
@Override
public void run() {
// TODO Auto-generated method stub
handler.sendEmptyMessage(_index);
_index++;
}
}
private class MyHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
try {
Bitmap bmp= BitmapFactory.decodeStream(Radio.this.getAssets().open("drum_"+_index+".png"));
_imagView.setImageBitmap(bmp);
Log.v("Loaing Image: ",_index+"");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v("Exception in Handler ",e.getMessage());
}
}}}