2013-01-21 57 views
0

當我從url中顯示圖像時,應用程序崩潰。我做錯了什麼? 請幫幫我。謝謝!Android - 設置imageView Url時出錯

我的佈局

<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" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 
    <ImageView 
     android:id="@+id/imageview" 
     android:layout_gravity="center" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scaleType="center" 
     /> 
</RelativeLayout> 

這是我的MainActivity.java。

private String imageFileURL = "http://www.google.com/images/nav_logo114.png"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ImageView myImageView = (ImageView)findViewById(R.id.imageview); 
    try { 
     URL url = new URL(imageFileURL); 
     URLConnection conn = url.openConnection(); 

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

     if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
      InputStream inputStream = httpConn.getInputStream(); 

      Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
      inputStream.close(); 
      myImageView.setImageBitmap(bitmap); 
     } 
    } catch (MalformedURLException e1) { 
     e1.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

在AndroidManifest.xml中,我添加

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

但是,它仍然顯示運行時錯誤和應用程序崩潰。

以下是logcat的

01-22 04:44:19.019: D/AndroidRuntime(4452): Shutting down VM 
01-22 04:44:19.019: W/dalvikvm(4452): threadid=1: thread exiting with uncaught exception (group=0x40aba228) 
01-22 04:44:19.029: E/AndroidRuntime(4452): FATAL EXCEPTION: main 
01-22 04:44:19.029: E/AndroidRuntime(4452): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.MainActivity}: android.os.NetworkOnMainThreadException 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at android.app.ActivityThread.access$600(ActivityThread.java:139) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at android.os.Looper.loop(Looper.java:154) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at android.app.ActivityThread.main(ActivityThread.java:4945) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at dalvik.system.NativeStart.main(Native Method) 
01-22 04:44:19.029: E/AndroidRuntime(4452): Caused by: android.os.NetworkOnMainThreadException 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at java.net.InetAddress.lookupHostByName(InetAddress.java:394) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at java.net.InetAddress.getAllByName(InetAddress.java:220) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at com.test.MainActivity.onCreate(MainActivity.java:77) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at android.app.Activity.performCreate(Activity.java:4531) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150) 
01-22 04:44:19.029: E/AndroidRuntime(4452):  ... 11 more 
+1

看到http://stackoverflow.com/questions/7740767/android-thread-exiting-with-uncaught-exception-group-0x4001d800 –

回答

2

你執行你的主UI線程的網絡操作。如果您的目標SDK爲11(Honeycomb)或更高,則會拋出NetworkOnMainThreadException,因爲此行爲可能會阻止UI並導致應用程序無響應。

你可以使用一個AsyncTask來解決這個問題,在其doInBackground(..)加載數據。

+0

謝謝。我已經解決了這個問題。我也看到http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Howard