0

私のAndroidアプリは、リンクから画像をロードするために使用されます。activity_main を表示するために実行できます。しかし、リンクを貼って画像をロードすると。クラッシュします。何度もチェックしていました。しかし、私はバグを見つけることができません。誰でも私を助けることができますか?

public class MainActivity extends Activity {
Button btt;
EditText edtext;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edtext = (EditText) this.findViewById(R.id.edtext);
    btt = (Button) this.findViewById(R.id.btt);
    btt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ImageView img;
            String url= edtext.getText().toString();
            Bitmap bitmap = DownloadImage(url);
            img = (ImageView) findViewById(R.id.imgview);
            img.setImageBitmap(bitmap);

        }
    });
}

private InputStream OpenHttpConnection(String urlString) throws IOException {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if(!(conn instanceof HttpURLConnection))
        throw new IOException("Not an http connection");
    try {
        HttpURLConnection http = (HttpURLConnection) conn;
        http.setAllowUserInteraction(false);
        http.setInstanceFollowRedirects(true);
        http.setRequestMethod("GET");
        http.connect();
        response = http.getResponseCode();
        if(response == HttpURLConnection.HTTP_OK) {
            in = http.getInputStream();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return in;
}
private Bitmap DownloadImage(String URL) {
    Bitmap bitmap = null;
    InputStream in = null;
    try {
    in = OpenHttpConnection(URL);
    bitmap = BitmapFactory.decodeStream(in);
    in.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return bitmap; 
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

私のmainactivity.java

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/edtext"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:text="http://" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btt"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="Load" />

<ImageView
    android:id="@+id/imgview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/edtext"
    android:layout_below="@+id/btt"
    android:layout_marginTop="24dp"
    android:src="@drawable/ic_launcher" />

activity_main.xml

02-23 21:17:15.508: E/AndroidRuntime(1270): FATAL EXCEPTION: main
02-23 21:17:15.508: E/AndroidRuntime(1270): java.lang.NullPointerException
02-23 21:17:15.508: E/AndroidRuntime(1270):     at com.example.images.MainActivity.DownloadImage(MainActivity.java:75)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at com.example.images.MainActivity.access$0(MainActivity.java:69)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at com.example.images.MainActivity$1.onClick(MainActivity.java:37)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at android.view.View.performClick(View.java:4084)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at android.view.View$PerformClick.run(View.java:16966)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at android.os.Handler.handleCallback(Handler.java:615)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at android.os.Looper.loop(Looper.java:137)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at android.app.ActivityThread.main(ActivityThread.java:4745)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at java.lang.reflect.Method.invokeNative(Native Method)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at java.lang.reflect.Method.invoke(Method.java:511)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-23 21:17:15.508: E/AndroidRuntime(1270):     at dalvik.system.NativeStart.main(Native Method)
4

2 に答える 2

-1

この線

img = (ImageView) findViewById(R.id.imgview);

に変更してみてください

View parent = (View)v.getParent();
if (parent != null) {   
   img = (ImageView) parent.findViewById(R.id.imgview);
}

findViewById が null を返していると推測しています。

于 2014-02-23T21:46:43.630 に答える
-1

My guess from your trace is that the method OpenHttpConnection is returning a null input stream.

If there is no other stack trace on the log, then the piece of code that is messing up with you is this one:

if(response == HttpURLConnection.HTTP_OK) {
     in = http.getInputStream();
}

If the image you're trying to get is not present, your InputStream will be null. You need to think about what you want to do whenever this happens (e.g. not setting the image?).

I'm not completely sure about it, but in case your image is cached, you might get a different response than HTTP 200 OK, it could be a "304 not modified", for example.

于 2014-02-23T21:56:50.107 に答える