我一直在尝试创建一个可以在加载进度条时检索图像的片段。但是当片段调用doWork()时,它会使应用程序崩溃。导入图像是注意问题。 我试图使用简单的东西,比如只显示Toast并崩溃。
(这在一项活动中非常有效,但在片段中却没有效果)
这是我的代码:
主要活动:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setLayout(View view){
PictureLoader pictureLoader = new PictureLoader();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragmentContainer, pictureLoader);
transaction.commit();
}
}
片段:
public class PictureLoader extends Fragment{
ImageView iv;
ProgressBar progress;
int progressStatus;
Handler mHandler;
int progressValue;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.photo_loader,container, false);
progress = (ProgressBar)view.findViewById(R.id.progressBar1);
mHandler = new Handler();
progress.setVisibility(View.GONE);
iv = (ImageView)view.findViewById(R.id.imageView1);
loadImage();
return view;
}
public void loadImage(){
progress.setVisibility(View.VISIBLE);
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus = doWork();
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
progress.setProgress(progressStatus);
}
});
}
if (progressStatus >= 100) {
progress.setVisibility(View.GONE);
}
}
private int doWork() {
File picturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File outputDir = new File(picturesDir, "All pictures");
Bitmap bitmap = BitmapFactory.decodeFile(outputDir.getPath() + "/" + "IMG_201407211_183804.jpg");
iv.setImageBitmap(bitmap);
Toast.makeText(getActivity(), "Done", Toast.LENGTH_LONG).show();
return 100;
}
}).start();
}
}
记录:
08-09 13:35:16.172: D/(4138): HostConnection::get() New Host Connection established 0xb9223bc0, tid 4138
08-09 13:35:16.172: D/libEGL(4138): loaded /system/lib/egl/libGLESv1_CM_emulation.so
08-09 13:35:16.192: D/libEGL(4138): loaded /system/lib/egl/libGLESv2_emulation.so
08-09 13:35:16.302: W/EGL_emulation(4138): eglSurfaceAttrib not implemented
08-09 13:35:16.362: D/OpenGLRenderer(4138): Enabling debug mode 0
08-09 13:35:20.402: D/dalvikvm(4138): GC_FOR_ALLOC freed 65K, 10% free 2852K/3140K, paused 68ms, total 71ms
08-09 13:35:20.512: D/dalvikvm(4138): GC_FOR_ALLOC freed 9K, 10% free 2975K/3272K, paused 48ms, total 49ms
08-09 13:35:20.522: I/dalvikvm-heap(4138): Grow heap (frag case) to 4.289MB for 1228812-byte allocation
08-09 13:35:20.592: D/dalvikvm(4138): GC_FOR_ALLOC freed <1K, 7% free 4174K/4476K, paused 65ms, total 65ms
08-09 13:35:20.722: W/dalvikvm(4138): threadid=11: thread exiting with uncaught exception (group=0xb0eaf648)
08-09 13:35:20.732: E/AndroidRuntime(4138): FATAL EXCEPTION: Thread-219
08-09 13:35:20.732: E/AndroidRuntime(4138): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-09 13:35:20.732: E/AndroidRuntime(4138): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5908)
08-09 13:35:20.732: E/AndroidRuntime(4138): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:837)
08-09 13:35:20.732: E/AndroidRuntime(4138): at android.view.View.requestLayout(View.java:15792)
08-09 13:35:20.732: E/AndroidRuntime(4138): at android.view.View.requestLayout(View.java:15792)
08-09 13:35:20.732: E/AndroidRuntime(4138): at android.view.View.requestLayout(View.java:15792)
08-09 13:35:20.732: E/AndroidRuntime(4138): at android.view.View.requestLayout(View.java:15792)
08-09 13:35:20.732: E/AndroidRuntime(4138): at android.view.View.requestLayout(View.java:15792)
08-09 13:35:20.732: E/AndroidRuntime(4138): at android.view.View.requestLayout(View.java:15792)
08-09 13:35:20.732: E/AndroidRuntime(4138): at android.view.View.requestLayout(View.java:15792)
08-09 13:35:20.732: E/AndroidRuntime(4138): at android.view.View.requestLayout(View.java:15792)
08-09 13:35:20.732: E/AndroidRuntime(4138): at android.widget.ImageView.setImageDrawable(ImageView.java:422)
08-09 13:35:20.732: E/AndroidRuntime(4138): at android.widget.ImageView.setImageBitmap(ImageView.java:437)
08-09 13:35:20.732: E/AndroidRuntime(4138): at com.example.photoloader.PictureLoader$1.doWork(PictureLoader.java:70)
08-09 13:35:20.732: E/AndroidRuntime(4138): at com.example.photoloader.PictureLoader$1.run(PictureLoader.java:49)
08-09 13:35:20.732: E/AndroidRuntime(4138): at java.lang.Thread.run(Thread.java:841)
我希望你能帮助我。谢谢
答案 0 :(得分:0)
此行从后台线程执行。但它触及了UI元素,因此会崩溃。
progress.setVisibility(View.GONE);
只有UI线程可以触摸UI小部件。
CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views
请改为:
if (progressStatus >= 100) {
mHandler.post(new Runnable() {
public void run() {
progress.setVisibility(View.GONE);
}
});
}
它不会崩溃,但你应该真的读到AsyncTask。