我正在构建一个Android应用程序并试图通过使用计时器类来实现幻灯片演示。第一个图像加载正常,然后我的应用程序崩溃。知道问题可能是什么?
import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.service.dreams.DreamService;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class screenSaver extends DreamService {
ArrayList<Bitmap> imageList = new ArrayList<Bitmap>();
int slideCounter = 0;
ImageView slide;
Cursor images;
private int counter = 0;
public screenSaver() {
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
setInteractive(false);
setFullscreen(true);
setContentView(R.layout.screen_saver);
databaseHelper dbHelper = new databaseHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
images = db.rawQuery("SELECT " + databaseHelper.IMAGE + " FROM " + databaseHelper.TABLE_NAME + " where " + databaseHelper.LTO + " = 1", null);
images.moveToFirst();
while(!images.isAfterLast()) {
imageList.add(BitmapFactory.decodeByteArray(images.getBlob(images.getColumnIndex(databaseHelper.IMAGE)), 0, images.getBlob(images.getColumnIndex(databaseHelper.IMAGE)).length ));
images.moveToNext();
}
}
@Override
public void onDreamingStarted() {
slide = (ImageView) findViewById(R.id.slider);
class flipper extends TimerTask {
@Override
public void run() {
slide.setImageBitmap(imageList.get(counter));
if(counter == (imageList.size())) {
counter = 0;
} else {
counter++;
}
}
}
Timer timer = new Timer();
timer.scheduleAtFixedRate(new flipper(), 0, 5000);
}
}
我一直在努力解决这个问题,所以我真的非常感谢任何指导。
非常感谢!
编辑:错误
Process: midamcorp.com.burgerkingapp, PID: 2126
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:942)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5081)
at android.view.View.invalidateInternal(View.java:12713)
at android.view.View.invalidate(View.java:12677)
at android.view.View.invalidate(View.java:12661)
at android.widget.ImageView.invalidateDrawable(ImageView.java:246)
at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:385)
at android.graphics.drawable.Drawable.setBounds(Drawable.java:165)
at android.widget.ImageView.configureBounds(ImageView.java:1116)
at android.widget.ImageView.updateDrawable(ImageView.java:898)
at android.widget.ImageView.setImageDrawable(ImageView.java:472)
at android.widget.ImageView.setImageBitmap(ImageView.java:605)
at midamcorp.com.burgerkingapp.MainActivity$1flipper.run(MainActivity.java:48)
at java.util.Timer$TimerImpl.run(Timer.java:284)
答案 0 :(得分:5)
从调试消息中,错误是由线程引起的,因为它与UI不在同一个线程中。
slide.setImageBitmap(imageList.get(counter));
可能的解决方案是使用Handler或runOnUIThread