2013-02-24 95 views
1

應用程序的主要目的是下載和顯示圖像,但是當我嘗試啓動應用程序時它崩潰。下載圖像並顯示它

這是我的代碼。

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對象被創建。我完全同意這一點。 任何幫助將不勝感激。

+0

你真的**讀過**錯誤嗎?它說:'NullPointerException:println需要一個消息',這似乎是你的'Log.e'語句的一個問題。請參閱[Bug-Report](http://code.google.com/p/android/issues/detail?id=9211) – 2013-02-24 11:21:39

+0

@Math Daimon嘗試我的解決方案,如果您有任何問題,請告訴我。 – 2013-02-24 12:04:41

+0

這幫了很多! – 2016-04-10 18:36:04

回答

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" /> 
+0

我不明白你的代碼將如何糾正錯誤:( – 2013-02-24 11:30:09

+0

我已經註釋掉了這行代碼,試圖啓動應用程序。它沒有崩潰,但圖像沒有出現:/ – 2013-02-24 11:42:42

+0

@Math Daimon嘗試我上面的代碼.. – 2013-02-24 11:46:42

1

我懷疑它會幫助,但你不(可能不應該)手工調用doInBackground的AsyncTask方法。 Android會自己做這件事。事實上,您的AsyncTask的整個邏輯有點關閉。 onPostExecute在doInBackground完成時被調用。

+0

實際上,嘗試將task.onPostexecute(...)更改爲task.execute(...),只傳遞您的url字符串。這應該有幫助! :) – LokiSinclair 2013-02-24 11:47:47

+0

我試過這個,沒有幫助:( task.execute(「http://parkcinema.az/uploads/structures/movies/images/xickok_poster1_resized.jpg」); – 2013-02-24 12:00:52