1

我從URL下載圖像並顯示在ImageView中。這個例子在2.3.3模擬器上運行良好,但是當我在4.0或4.1上嘗試它時,它與消息崩潰:下載圖像在2.3.3下工作,但不能在4.0或4.1下工作

不幸的是,AndroidWebImageExample已停止。

LogCat顯示我收到NullPointerException。任何人都知道爲什麼它不適用於4.0或4.1?

MainActivity.java

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.widget.ImageView; 

public class MainActivity extends Activity { 

    String image_URL = "http://4.bp.blogspot.com/_C5a2qH8Y_jk/StYXDpZ9-WI/AAAAAAAAAJQ/sCgPx6jfWPU/S1600-R/android.png"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ImageView bmImage = (ImageView) findViewById(R.id.image); 
     BitmapFactory.Options bmOptions; 
     bmOptions = new BitmapFactory.Options(); 
     bmOptions.inSampleSize = 1; 
     Bitmap bm = LoadImage(image_URL, bmOptions); 
     bmImage.setImageBitmap(bm); 
    } 

    private Bitmap LoadImage(String URL, BitmapFactory.Options options) { 
     Bitmap bitmap = null; 
     InputStream in = null; 
     try { 
      in = OpenHttpConnection(URL); 
      bitmap = BitmapFactory.decodeStream(in, null, options); 
      in.close(); 
     } catch (IOException e1) { 
     } 
     return bitmap; 
    } 

    private InputStream OpenHttpConnection(String strURL) throws IOException { 
     InputStream inputStream = null; 
     URL url = new URL(strURL); 
     URLConnection conn = url.openConnection(); 

     try { 
      HttpURLConnection httpConn = (HttpURLConnection) conn; 
      httpConn.setRequestMethod("GET"); 
      httpConn.connect(); 

      if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       inputStream = httpConn.getInputStream(); 
      } 
     } catch (Exception ex) { 
     } 
     return inputStream; 
    } 

} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="hello" 
    /> 
<ImageView 
    android:id="@+id/image" 
    android:scaleType="centerCrop" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
/> 
</LinearLayout> 

logcat的

07-20 14:09:08.689: D/AndroidRuntime(1894): Shutting down VM 
07-20 14:09:08.689: W/dalvikvm(1894): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
07-20 14:09:08.699: E/AndroidRuntime(1894): FATAL EXCEPTION: main 
07-20 14:09:08.699: E/AndroidRuntime(1894): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidwebimageexample/com.example.androidwebimageexample.MainActivity}: java.lang.NullPointerException 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at android.os.Looper.loop(Looper.java:137) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at java.lang.reflect.Method.invoke(Method.java:511) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at dalvik.system.NativeStart.main(Native Method) 
07-20 14:09:08.699: E/AndroidRuntime(1894): Caused by: java.lang.NullPointerException 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at com.example.androidwebimageexample.MainActivity.LoadImage(MainActivity.java:43) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at com.example.androidwebimageexample.MainActivity.onCreate(MainActivity.java:33) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at android.app.Activity.performCreate(Activity.java:5008) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
07-20 14:09:08.699: E/AndroidRuntime(1894):  ... 11 more 
+0

你不能在UI線程上運行網絡操作4.0 – xiaowl 2012-08-09 12:28:58

回答

1

您正在主線程上執行(可能較慢)的網絡操作。如果您的目標SDK爲11(Honeycomb)或更高,則會在Honeycomb或更高版本上拋出NetworkOnMainThreadException,因爲此行爲可能會阻止UI並導致應用程序無響應。即使在預蜂窩設備this behaviour is discouraged

您可以使用AsyncTask解決此問題,將數據加載到doInBackground(..)中。

+0

你是說我應該同時在AsyncTask的doInBackground()中使用LoadImage和OpenHTTPConnection?並返回onPostExecute()中的inputStream? – user1006896 2012-08-09 20:42:18

+0

是的。這可行 – Ahmad 2012-08-11 09:23:06

1

由於某種原因,InputStream爲NULL,因此會拋出NullPointerExeption。在你的OpenHttpConnection方法中,添加catch Log.i("OpenHttpConnection",ex.getMessage());並查看原因。

不要阻塞UI線程,您必須使用AsyncTask,一個服務或自定義線程。 您想獲得這個課程http://code.google.com/p/and-bookworm/source/browse/trunk/src/com/totsp/bookworm/data/HTTPRequestHelper.java?r=31。 在構造函數中傳遞一個自定義的ResponseHandler並進行所需的操作。

相關問題