【问题标题】:Download image and display it下载图片并显示
【发布时间】:2013-02-24 20:09:24
【问题描述】:

应用程序的主要目的是下载和显示图像,但是当我尝试启动应用程序时它崩溃了。

这是我的代码。

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 VM
02-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: main
02-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 message
02-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.access$600(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 message
02-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 对象。我对此完全感到困惑。 任何帮助将不胜感激。

【问题讨论】:

  • 您真的阅读这个错误吗?它说:NullPointerException: println needs a message,这似乎是您的Log.e-statement 的问题。见Bug-Report
  • @Math Daimon 试试我的解决方案,如果有任何问题,请告诉我。

标签: android


【解决方案1】:

NPE 的原因(空指针异常)

Log.e("错误", e.getMessage()); //e.getMessage() 为 NULL,所以它给出了 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;
}

在AndroidManifest.xml中添加这个权限,因为你是在做互联网操作。

<uses-permission android:name="android.permission.INTERNET" />

【讨论】:

  • 我已经注释掉了这行代码,尝试启动应用。它没有崩溃,但没有出现图像:/
  • urldisplay 是字符串,你试图在字符串上调用 openConnection() 方法。
  • @Math Daimon 现在检查一下。
  • @MathDaimon 我已经检查了 logcat 请按照我上面所说的添加权限并接受它可以解决您的问题。
  • 非常感谢!它有帮助:))
【解决方案2】:

我怀疑它会有所帮助,但您不(可能不应该)手动调用 doInBackground AsyncTask 方法。 Android 会自己做这件事。事实上,你的 AsyncTask 的整个逻辑有点偏离。 onPostExecute 在 doInBackground 完成时调用。

【讨论】:

猜你喜欢
  • 2012-10-30
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多