我的 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)