现在iam开发android照片graphy应用程序,因为我想使用AsyncTask保存图片。首先,我想要使用onPreExecute()显示进度对话框,并且覆盖图像将在doInBackground()和fianlly中执行想要保存图像我将在onPostExecute()上编写调用save()。但是当点击保存按钮时,它会收到强制关闭错误说...
This is my logcat file
11-17 12:33:56.689: E/AndroidRuntime(8753): FATAL EXCEPTION: AsyncTask #1
11-17 12:33:56.689: E/AndroidRuntime(8753): java.lang.RuntimeException: An error occured while executing doInBackground()
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.lang.Thread.run(Thread.java:1019)
11-17 12:33:56.689: E/AndroidRuntime(8753): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.ViewRoot.checkThread(ViewRoot.java:3035)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.ViewRoot.requestLayout(ViewRoot.java:636)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.View.requestLayout(View.java:8267)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.View.requestLayout(View.java:8267)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.View.requestLayout(View.java:8267)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.View.requestLayout(View.java:8267)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.View.requestLayout(View.java:8267)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.widget.ImageView.setImageDrawable(ImageView.java:322)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.widget.ImageView.setImageBitmap(ImageView.java:336)
11-17 12:33:56.689: E/AndroidRuntime(8753): at com.outthinking.instatext.share.apllyEffect(share.java:145)
11-17 12:33:56.689: E/AndroidRuntime(8753): at com.outthinking.instatext.share.access$0(share.java:122)
11-17 12:33:56.689: E/AndroidRuntime(8753): at com.outthinking.instatext.share$AsyncClass.doInBackground(share.java:106)
11-17 12:33:56.689: E/AndroidRuntime(8753): at com.outthinking.instatext.share$AsyncClass.doInBackground(share.java:1)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
11-17 12:33:56.689: E/AndroidRuntime(8753): ... 4 more
11-17 12:33:56.689: E/AndroidRuntime(8753): [Blue Error Handler] Make Debugging Report file for AsyncTask #1
11-17 12:33:56.689: E/AndroidRuntime(8753): java.lang.RuntimeException: An error occured while executing doInBackground()
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.lang.Thread.run(Thread.java:1019)
11-17 12:33:56.689: E/AndroidRuntime(8753): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.ViewRoot.checkThread(ViewRoot.java:3035)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.ViewRoot.requestLayout(ViewRoot.java:636)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.View.requestLayout(View.java:8267)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.View.requestLayout(View.java:8267)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.View.requestLayout(View.java:8267)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.View.requestLayout(View.java:8267)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.view.View.requestLayout(View.java:8267)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.widget.ImageView.setImageDrawable(ImageView.java:322)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.widget.ImageView.setImageBitmap(ImageView.java:336)
11-17 12:33:56.689: E/AndroidRuntime(8753): at com.outthinking.instatext.share.apllyEffect(share.java:145)
11-17 12:33:56.689: E/AndroidRuntime(8753): at com.outthinking.instatext.share.access$0(share.java:122)
11-17 12:33:56.689: E/AndroidRuntime(8753): at com.outthinking.instatext.share$AsyncClass.doInBackground(share.java:106)
11-17 12:33:56.689: E/AndroidRuntime(8753): at com.outthinking.instatext.share$AsyncClass.doInBackground(share.java:1)
11-17 12:33:56.689: E/AndroidRuntime(8753): at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-17 12:33:56.689: E/AndroidRuntime(8753): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
11-17 12:33:56.689: E/AndroidRuntime(8753): ... 4 more
11-17 12:33:57.279: E/WindowManager(8753): [WindowManagerImpl.java:290:closeAll()] Activity com.outthinking.instatext.share has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40543970 that was originally added here
11-17 12:33:57.279: E/WindowManager(8753): android.view.WindowLeaked: Activity com.outthinking.instatext.share has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40543970 that was originally added here
11-17 12:33:57.279: E/WindowManager(8753): at android.view.ViewRoot.<init>(ViewRoot.java:265)
11-17 12:33:57.279: E/WindowManager(8753): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
11-17 12:33:57.279: E/WindowManager(8753): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
11-17 12:33:57.279: E/WindowManager(8753): at android.view.Window$LocalWindowManager.addView(Window.java:424)
11-17 12:33:57.279: E/WindowManager(8753): at android.app.Dialog.show(Dialog.java:241)
11-17 12:33:57.279: E/WindowManager(8753): at com.outthinking.instatext.share$AsyncClass.onPreExecute(share.java:96)
11-17 12:33:57.279: E/WindowManager(8753): at android.os.AsyncTask.execute(AsyncTask.java:391)
11-17 12:33:57.279: E/WindowManager(8753): at com.outthinking.instatext.share.onClick(share.java:182)
11-17 12:33:57.279: E/WindowManager(8753): at android.view.View.performClick(View.java:2485)
11-17 12:33:57.279: E/WindowManager(8753): at android.view.View$PerformClick.run(View.java:9080)
11-17 12:33:57.279: E/WindowManager(8753): at android.os.Handler.handleCallback(Handler.java:587)
11-17 12:33:57.279: E/WindowManager(8753): at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 12:33:57.279: E/WindowManager(8753): at android.os.Looper.loop(Looper.java:130)
11-17 12:33:57.279: E/WindowManager(8753): at android.app.ActivityThread.main(ActivityThread.java:3737)
11-17 12:33:57.279: E/WindowManager(8753): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 12:33:57.279: E/WindowManager(8753): at java.lang.reflect.Method.invoke(Method.java:507)
11-17 12:33:57.279: E/WindowManager(8753): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:894)
11-17 12:33:57.279: E/WindowManager(8753): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
11-17 12:33:57.279: E/WindowManager(8753): at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
public class AsyncClass extends AsyncTask<Void, String, Bitmap> {
private Context context;
ProgressDialog dialog;
public AsyncClass(Context cxt) {
context = cxt;
dialog = new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
dialog.setTitle("Please wait save the image...");
dialog.show();
}
@Override
protected Bitmap doInBackground(Void... unused) {
//Bitmap original = getResizedBitmap(ImageViewShow.bitmap_img, sharewidth,shareheight);
Bitmap bmp1 = apllyEffect();
//if()
return bmp1;
}
@Override
protected void onPostExecute(Bitmap bmp) {
String fieName = UUID.randomUUID().toString();
SaveImage(fieName, 100,bmp);
dialog.dismiss();
}
}
and save code is...
private Bitmap apllyEffect() {
if(second.frame_bitmap==null)
{
second.theam_bitmap = BitmapFactory.decodeResource(getResources(), second.current_theam);
second.bmOverlay = Bitmap.createBitmap(second.canvas.getWidth(), second.canvas.getHeight(), second.theam_bitmap.getConfig());
second.canvas = new Canvas(second.bmOverlay);
second.canvas.setBitmap(second.bmOverlay);
second.canvas.drawBitmap(second.theam_bitmap, new Matrix(), null);
Typeface typeface=stylesapply(second.typefacevalue);
share_tv.setTypeface(typeface);
share_tv.setTextColor(second.current_color);
share_img.setImageBitmap(second.bmOverlay);
}else{
second.theam_bitmap= BitmapFactory.decodeResource(getResources(), second.current_theam);
second.frame_bitmap = BitmapFactory.decodeResource(getResources(), second.current_frame);
second.bmOverlay = Bitmap.createBitmap(second.canvas.getWidth(), second.canvas.getHeight(), second.theam_bitmap.getConfig());
second.canvas = new Canvas(second.bmOverlay);
second.canvas.setBitmap(second.bmOverlay);
second.canvas.drawBitmap(second.theam_bitmap, new Matrix(), null);
second.canvas.drawBitmap(second.frame_bitmap, new Matrix(), null);
share_tv.setTextColor(second.current_color);
share_img.setImageBitmap(second.bmOverlay);
}
return second.bmOverlay;
}
答案 0 :(得分:1)
看起来您正在尝试从ASyncTask执行UI操作。
您应该只从onPreExecute
,onPostExecute
和onProgressUpdate
执行UI操作。
在doInBackground中,您不应直接访问UI元素。
检查第106行的share.java中的代码,该代码正在调用第145行...
当你这样做时
share_img.setImageBitmap(...)
这绝对是UI操作......你应该把它移到其他地方。以及share_tv
使用onProgressUpdate
,如果它只在doInBackground
的末尾,则可以将其移至onPostExecute
。否则,您也可以使用runOnUiThread
答案 1 :(得分:1)
您只能从主线程调用UI操作。 doInBackground()
使用它自己的线程。处理AsyncTask的进度覆盖onProgressUpdate()
方法。
答案 2 :(得分:0)
- doInBackground()
方法用于执行非UI线程上的非UI工作,以及onPost()
方法来执行 UI在专用UI线程上工作。
- 我认为您正试图在doInBackground()
方法上执行用户界面工作,这会导致其失败。
答案 3 :(得分:0)
您无法在后台线程中执行UI操作。如果您这样做,您将获得android.view.ViewRoot$CalledFromWrongThreadException
。
将您的UI操作移至onPostExecute()
方法。