android-下载图像并显示

android-下载图像并显示,第1张

概述应用程序的主要目的是下载显示图像,但是当我尝试启动应用程序时会崩溃.这是我的代码.privateDownloadImageTasktask;protectedvoidonCreate(BundlesavedInstanceState){task=newDownloadImageTask();task.onPostExecute(task.doInBackground("http://par

应用程序的主要目的是下载并显示图像,但是当我尝试启动应用程序时会崩溃.

这是我的代码.

private DownloadImageTask task;protected voID onCreate(Bundle savedInstanceState) {    task = new DownloadImageTask();    task.onPostExecute(task.doInBackground("http://parkcinema.az/uploads/structures/movIEs/images/xickok_poster1_resized.jpg"));    }private class DownloadImageTask extends AsyncTask <String, VoID, Bitmap> {    protected Bitmap doInBackground(String... urls) {        String urldisplay = urls[0];        Bitmap mIcon11 = null;        try {            inputStream in = new java.net.URL(urldisplay).openStream();            mIcon11 = BitmapFactory.decodeStream(in);        } catch (Exception e) {        Log.e("Error", e.getMessage());        e.printstacktrace();    }    return mIcon11;}        protected voID onPostExecute(Bitmap result) {      ImageVIEw img = (ImageVIEw) findVIEwByID(R.ID.imageVIEw1);      img.setimageBitmap(result);           }}

这是LogCat:

02-24 11:04:56.814: E/Trace(957): error opening trace file: No such file or directory (2)02-24 11:04:57.384: D/AndroIDRuntime(957): Shutting down VM02-24 11:04:57.384: W/dalvikvm(957): threadID=1: thread exiting with uncaught exception (group=0x40a13300)02-24 11:04:57.404: E/AndroIDRuntime(957): FATAL EXCEPTION: main02-24 11:04:57.404: E/AndroIDRuntime(957): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bakumovIEs/com.example.bakumovIEs.MainActivity}: java.lang.NullPointerException: println needs a message02-24 11:04:57.404: E/AndroIDRuntime(957):  at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2059)02-24 11:04:57.404: E/AndroIDRuntime(957):  at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)02-24 11:04:57.404: E/AndroIDRuntime(957):  at androID.app.ActivityThread.access0(ActivityThread.java:130)02-24 11:04:57.404: E/AndroIDRuntime(957):  at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)02-24 11:04:57.404: E/AndroIDRuntime(957):  at androID.os.Handler.dispatchMessage(Handler.java:99)02-24 11:04:57.404: E/AndroIDRuntime(957):  at androID.os.Looper.loop(Looper.java:137)02-24 11:04:57.404: E/AndroIDRuntime(957):  at androID.app.ActivityThread.main(ActivityThread.java:4745)02-24 11:04:57.404: E/AndroIDRuntime(957):  at java.lang.reflect.Method.invokeNative(Native Method)02-24 11:04:57.404: E/AndroIDRuntime(957):  at java.lang.reflect.Method.invoke(Method.java:511)02-24 11:04:57.404: E/AndroIDRuntime(957):  at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)02-24 11:04:57.404: E/AndroIDRuntime(957):  at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:553)02-24 11:04:57.404: E/AndroIDRuntime(957):  at dalvik.system.NativeStart.main(Native Method)02-24 11:04:57.404: E/AndroIDRuntime(957): Caused by: java.lang.NullPointerException: println needs a message02-24 11:04:57.404: E/AndroIDRuntime(957):  at androID.util.Log.println_native(Native Method)02-24 11:04:57.404: E/AndroIDRuntime(957):  at androID.util.Log.e(Log.java:231)02-24 11:04:57.404: E/AndroIDRuntime(957):  at com.example.bakumovIEs.MainActivity$DownloadImageTask.doInBackground(MainActivity.java:49)02-24 11:04:57.404: E/AndroIDRuntime(957):  at com.example.bakumovIEs.MainActivity.onCreate(MainActivity.java:27)02-24 11:04:57.404: E/AndroIDRuntime(957):  at androID.app.Activity.performCreate(Activity.java:5008)02-24 11:04:57.404: E/AndroIDRuntime(957):  at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)02-24 11:04:57.404: E/AndroIDRuntime(957):  at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2023)02-24 11:04:57.404: E/AndroIDRuntime(957):  ... 11 more

这是.xml

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    tools:context=".MainActivity" >    <ImageVIEw        androID:ID="@+ID/imageVIEw1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignParentleft="true"        androID:layout_centerVertical="true"        androID:layout_marginleft="144dp" /></relativeLayout>

我不明白为什么会崩溃.在新线程中启动图像下载,创建了imagevIEw对象.我对此完全感到困惑.
任何帮助将不胜感激.

解决方法:

NPE的原因(空指针异常)

Log.e(“Error”, e.getMessage()); //e.getMessage() is NulL so its giving NPE.

protected voID onCreate(Bundle savedInstanceState)  task = new DownloadImageTask().execute("http://parkcinema.az/uploads/structures/movIEs/images/xickok_poster1_resized.jpg");}protected Bitmap doInBackground(String... urls) {    String urldisplay = urls[0];    Bitmap mIcon11 = null;    try {        mIcon11 = BitmapFactory.decodeStream(new URL(urldisplay).openConnection().getinputStream());    }     catch (Exception e) {        e.printstacktrace();    }    return mIcon11;}

ADD this permission in AndroIDManifest.xml because you are doing internet operation.

<uses-permission androID:name="androID.permission.INTERNET" />
总结

以上是内存溢出为你收集整理的android-下载图像并显示全部内容,希望文章能够帮你解决android-下载图像并显示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1095605.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存