致命异常主要nullpointerexception如何解决?

时间:2014-02-23 21:38:11

标签: java android exception httpconnection null-pointer

我的Android应用程序用于从链接加载图像。它可以运行以显示activity_main。但是当我把链接和加载图像。它崩溃了。我经常多次检查。但我找不到bug。有人可以帮帮我吗?

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)

2 个答案:

答案 0 :(得分:-1)

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

尝试更改为

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

我猜测findViewById正在返回null。

答案 1 :(得分:-1)

我的跟踪猜测是OpenHttpConnection方法返回一个空输入流。

如果日志中没有其他堆栈跟踪,那么与您混淆的代码就是这样:

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

如果您尝试获取的图像不存在,则您的InputStream将为null。每当发生这种情况时你需要考虑你想做什么(例如没有设置图像?)。

我不是很确定,但是如果您的图像被缓存,您可能会获得与HTTP 200不同的响应,例如,它可能是“304未修改”。