0

my android app is used to load a image from a link. It can run to show activity_main. But when i put link and load image. It crash. I was check it all many times. But i can't find bug. Anyone can help me?

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;
}

}

my 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)
donfuxx
  • 11,277
  • 6
  • 44
  • 76
  • which line is: `MainActivity.java:75` ? – donfuxx Feb 23 '14 at 22:02
  • 1
    Ok let me be more clear: There are no source code line numbers displayed in stackoverflow and to analyze the problem I need to know what is in this line: MainActivity: 75. Could you please post it? I don't want to speculate like others.. – donfuxx Feb 23 '14 at 22:16

2 Answers2

-1

the line

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

try changing to

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

I'm guessing that findViewById is returning null.

JamesSugrue
  • 14,891
  • 10
  • 61
  • 93
-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.

Bruno Fondevila
  • 159
  • 1
  • 2