소스는 다음과 같습니다. 메세지를 받으면 for 루프를 통해 변경되는 인덱스를 가지고 리스트에 기록된 파일명을 받아와서 bitmap 객체를 완성하고 이미지 뷰를 통해 출력시킵니다.
그런데 이 코드를 수행하면 에러가 발생합니다. 스레드 구현하는 어느부분에서 잘못 된 것일까요...
Beginning Android의 thread 예제를 고쳤을 뿐인데...
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.Toast;
public class SlidePicture extends Activity {
ArrayList<String> FileLists = new ArrayList<String>(); // 파일명이 들어 있는 ArrayList
String FilePath;
int Index = 0; // ArrayList내에서 Index로 사용할 변수
ImageView iv;
Bitmap myBitmap;
Intent myLocalIntent;
Bundle myBundle;
// 핸들 정의
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
myBitmap = BitmapFactory.decodeFile(FileLists.get(Index)); // 파일이 기록된 경로명을 이용해 Bitmap생성
iv.setImageBitmap(myBitmap); // 이미지 뷰에서 비트맵 출력
} catch(Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
};
boolean isRunning = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slidepicture);
// 이전 Activity에서 보내준 리스트를 받기 위해 사용
myLocalIntent = getIntent();
FileLists = myBundle.getStringArrayList("FileLists");
iv = (ImageView)findViewById(R.id.SlideView);
}
public void onStart() {
super.onStart();
Thread backGround = new Thread(new Runnable() {
@Override
public void run() {
try { // 리스트의 끝까지 가는데 1초마다 thread는 sleep
for(Index = 0; Index < FileLists.size() && isRunning; Index++) {
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
}
catch(Throwable t) {
//finish();
}
}
});
isRunning = true;
backGround.start();
}
public void onStop() {
super.onStop();
finish();
}
}



