Android - 设置imageView Url时出错

时间:2013-01-21 20:57:46

标签: android android-imageview

当我从网址显示图片时,应用程序崩溃了。我做错了什么? 请帮我。谢谢!

我的布局

<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 个答案:

答案 0 :(得分:2)

您正在主UI线程上执行网络操作。如果您的目标SDK是11(Honeycomb)或更高版本,则会抛出NetworkOnMainThreadException,因为此行为可能会阻止用户界面并导致应用无响应。

您可以使用AsyncTask解决此问题,在doInBackground(..)中加载数据。