我使用 Google Chart API 在我的应用程序中构建图表。
就我而言,我需要在每个图表上下载图像。我为此使用 AsyncTask。
要监控下载过程,我使用进度对话框。但是在此类 AsyncTask 中出现错误。
AsyncTask 的执行:
new loadChart(ChartAct.this,img).execute();
和 AsyncTask 的代码:
public class loadChart extends AsyncTask<Void, Void, Void> {
private ImageView img;
private Context con;
private ProgressDialog dialog;
public loadChart(Context context, ImageView img1) {
this.img = img1;
this.con = context;
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(con, "Connecting:",
"Loading. Please wait...", true);
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
final Bitmap bitmap = DownloadImage();
img.setImageBitmap(bitmap);
return null;
}
@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
super.onPostExecute(result);
}
private static InputStream OpenHttpConnection(String urlString)
throws IOException {
Log.d("palval", "OpenHttpConnection");
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
String res = Integer.toString(response);
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
public static Bitmap DownloadImage() {
Log.d("palval", "DownloadImage");
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection("https://chart.googleapis.com/chart?chs=440x220&chd=t:60,40&cht=p3&chl=Hello|World");
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
}
错误:
09-06 18:35:35.574: E/AndroidRuntime(2502): FATAL EXCEPTION: AsyncTask #1
09-06 18:35:35.574: E/AndroidRuntime(2502): java.lang.RuntimeException: An error occured while executing doInBackground()
09-06 18:35:35.574: E/AndroidRuntime(2502): at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-06 18:35:35.574: E/AndroidRuntime(2502): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-06 18:35:35.574: E/AndroidRuntime(2502): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-06 18:35:35.574: E/AndroidRuntime(2502): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-06 18:35:35.574: E/AndroidRuntime(2502): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-06 18:35:35.574: E/AndroidRuntime(2502): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-06 18:35:35.574: E/AndroidRuntime(2502): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-06 18:35:35.574: E/AndroidRuntime(2502): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-06 18:35:35.574: E/AndroidRuntime(2502): at java.lang.Thread.run(Thread.java:864)
09-06 18:35:35.574: E/AndroidRuntime(2502): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-06 18:35:35.574: E/AndroidRuntime(2502): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4132)
09-06 18:35:35.574: E/AndroidRuntime(2502): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:723)
09-06 18:35:35.574: E/AndroidRuntime(2502): at android.view.View.requestLayout(View.java:12957)
09-06 18:35:35.574: E/AndroidRuntime(2502): at android.view.View.requestLayout(View.java:12957)
09-06 18:35:35.574: E/AndroidRuntime(2502): at android.view.View.requestLayout(View.java:12957)
09-06 18:35:35.574: E/AndroidRuntime(2502): at android.view.View.requestLayout(View.java:12957)
09-06 18:35:35.574: E/AndroidRuntime(2502): at android.view.View.requestLayout(View.java:12957)
09-06 18:35:35.574: E/AndroidRuntime(2502): at android.widget.ImageView.setImageDrawable(ImageView.java:362)
09-06 18:35:35.574: E/AndroidRuntime(2502): at android.widget.ImageView.setImageBitmap(ImageView.java:377)
09-06 18:35:35.574: E/AndroidRuntime(2502): at com.example.headache.loadChart.doInBackground(loadChart.java:37)
09-06 18:35:35.574: E/AndroidRuntime(2502): at com.example.headache.loadChart.doInBackground(loadChart.java:1)
09-06 18:35:35.574: E/AndroidRuntime(2502): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-06 18:35:35.574: E/AndroidRuntime(2502): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-06 18:35:35.574: E/AndroidRuntime(2502): ... 5 more
09-06 18:35:36.055: E/WindowManager(2502): Activity com.example.headache.ChartAct has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40dd3b28 that was originally added here
09-06 18:35:36.055: E/WindowManager(2502): android.view.WindowLeaked: Activity com.example.headache.ChartAct has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40dd3b28 that was originally added here
09-06 18:35:36.055: E/WindowManager(2502): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:352)
09-06 18:35:36.055: E/WindowManager(2502): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:373)
09-06 18:35:36.055: E/WindowManager(2502): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:321)
09-06 18:35:36.055: E/WindowManager(2502): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:152)
09-06 18:35:36.055: E/WindowManager(2502): at android.view.Window$LocalWindowManager.addView(Window.java:541)
09-06 18:35:36.055: E/WindowManager(2502): at android.app.Dialog.show(Dialog.java:301)
09-06 18:35:36.055: E/WindowManager(2502): at android.app.ProgressDialog.show(ProgressDialog.java:116)
09-06 18:35:36.055: E/WindowManager(2502): at android.app.ProgressDialog.show(ProgressDialog.java:99)
09-06 18:35:36.055: E/WindowManager(2502): at com.example.headache.loadChart.onPreExecute(loadChart.java:29)
09-06 18:35:36.055: E/WindowManager(2502): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
09-06 18:35:36.055: E/WindowManager(2502): at android.os.AsyncTask.execute(AsyncTask.java:511)
09-06 18:35:36.055: E/WindowManager(2502): at com.example.headache.ChartAct.onCreate(ChartAct.java:31)
09-06 18:35:36.055: E/WindowManager(2502): at android.app.Activity.performCreate(Activity.java:4531)
09-06 18:35:36.055: E/WindowManager(2502): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
09-06 18:35:36.055: E/WindowManager(2502): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
09-06 18:35:36.055: E/WindowManager(2502): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
09-06 18:35:36.055: E/WindowManager(2502): at android.app.ActivityThread.access$600(ActivityThread.java:139)
09-06 18:35:36.055: E/WindowManager(2502): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
09-06 18:35:36.055: E/WindowManager(2502): at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 18:35:36.055: E/WindowManager(2502): at android.os.Looper.loop(Looper.java:154)
09-06 18:35:36.055: E/WindowManager(2502): at android.app.ActivityThread.main(ActivityThread.java:4945)
09-06 18:35:36.055: E/WindowManager(2502): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 18:35:36.055: E/WindowManager(2502): at java.lang.reflect.Method.invoke(Method.java:511)
09-06 18:35:36.055: E/WindowManager(2502): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-06 18:35:36.055: E/WindowManager(2502): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-06 18:35:36.055: E/WindowManager(2502): at dalvik.system.NativeStart.main(Native Method)