大部分代码来自Download image and display it,所以当它没有工作时我感到很惊讶。我四处搜索,无法弄清楚
这里的" mainactivity" .java在这种情况下称为LleUrlEnter.java
package lle.imagedownloader;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.net.URL;
import android.widget.ImageView;
public class LleUrlEnter extends Activity {
private ImgDownloadTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String imgLink = "http://imgur.com/8WzhDgI";
task = new ImgDownloadTask();
task.onPostExecute(task.doInBackground(imgLink));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.lle_url_enter, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class ImgDownloadTask extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String...urls) {
String link = urls[0];
Bitmap imgBitmap = null;
try {
imgBitmap = BitmapFactory.decodeStream(new URL(link).openConnection().getInputStream());
}
catch (Exception e) {
e.printStackTrace();
}
return imgBitmap;
}
protected void onPostExecute(Bitmap bitmapImage) {
ImageView bitImg = (ImageView) findViewById(R.id.imageView1);
bitImg.setImageBitmap(bitmapImage);
}
}
}
然后是&#34; main.xml&#34;名为activity_lle_url_enter.xml
<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=".LleUrlEnter" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="144dp" />
然后最后是android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lle.imagedownloader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".LleUrlEnter"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这就是LogCat向我展示的
07-16 16:27:29.119: E/Trace(11735): error opening trace file: No such file or directory (2)
07-16 16:27:29.166: W/System.err(11735): android.os.NetworkOnMainThreadException
07-16 16:27:29.166: W/System.err(11735): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1131)
07-16 16:27:29.166: W/System.err(11735): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-16 16:27:29.166: W/System.err(11735): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-16 16:27:29.166: W/System.err(11735): at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-16 16:27:29.166: W/System.err(11735): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
07-16 16:27:29.166: W/System.err(11735): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
07-16 16:27:29.166: W/System.err(11735): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
07-16 16:27:29.166: W/System.err(11735): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
07-16 16:27:29.166: W/System.err(11735): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-16 16:27:29.166: W/System.err(11735): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
07-16 16:27:29.166: W/System.err(11735): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
07-16 16:27:29.166: W/System.err(11735): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
07-16 16:27:29.166: W/System.err(11735): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
07-16 16:27:29.166: W/System.err(11735): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
07-16 16:27:29.166: W/System.err(11735): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
07-16 16:27:29.166: W/System.err(11735): at lle.imagedownloader.LleUrlEnter$ImgDownloadTask.doInBackground(LleUrlEnter.java:53)
07-16 16:27:29.166: W/System.err(11735): at lle.imagedownloader.LleUrlEnter.onCreate(LleUrlEnter.java:23)
07-16 16:27:29.166: W/System.err(11735): at android.app.Activity.performCreate(Activity.java:5058)
07-16 16:27:29.166: W/System.err(11735): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-16 16:27:29.166: W/System.err(11735): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-16 16:27:29.166: W/System.err(11735): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
07-16 16:27:29.166: W/System.err(11735): at android.app.ActivityThread.access$700(ActivityThread.java:141)
07-16 16:27:29.166: W/System.err(11735): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
07-16 16:27:29.166: W/System.err(11735): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 16:27:29.166: W/System.err(11735): at android.os.Looper.loop(Looper.java:137)
07-16 16:27:29.166: W/System.err(11735): at android.app.ActivityThread.main(ActivityThread.java:5059)
07-16 16:27:29.166: W/System.err(11735): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 16:27:29.166: W/System.err(11735): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 16:27:29.166: W/System.err(11735): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
07-16 16:27:29.166: W/System.err(11735): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-16 16:27:29.166: W/System.err(11735): at dalvik.system.NativeStart.main(Native Method)
07-16 16:27:29.197: D/AndroidRuntime(11735): Shutting down VM
07-16 16:27:29.197: W/dalvikvm(11735): threadid=1: thread exiting with uncaught exception (group=0x40ebd2a0)
07-16 16:27:29.197: E/AndroidRuntime(11735): FATAL EXCEPTION: main
07-16 16:27:29.197: E/AndroidRuntime(11735): java.lang.RuntimeException: Unable to start activity ComponentInfo{lle.imagedownloader/lle.imagedownloader.LleUrlEnter}: java.lang.NullPointerException
07-16 16:27:29.197: E/AndroidRuntime(11735): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
07-16 16:27:29.197: E/AndroidRuntime(11735): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
07-16 16:27:29.197: E/AndroidRuntime(11735): at android.app.ActivityThread.access$700(ActivityThread.java:141)
07-16 16:27:29.197: E/AndroidRuntime(11735): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
07-16 16:27:29.197: E/AndroidRuntime(11735): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 16:27:29.197: E/AndroidRuntime(11735): at android.os.Looper.loop(Looper.java:137)
07-16 16:27:29.197: E/AndroidRuntime(11735): at android.app.ActivityThread.main(ActivityThread.java:5059)
07-16 16:27:29.197: E/AndroidRuntime(11735): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 16:27:29.197: E/AndroidRuntime(11735): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 16:27:29.197: E/AndroidRuntime(11735): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
07-16 16:27:29.197: E/AndroidRuntime(11735): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-16 16:27:29.197: E/AndroidRuntime(11735): at dalvik.system.NativeStart.main(Native Method)
07-16 16:27:29.197: E/AndroidRuntime(11735): Caused by: java.lang.NullPointerException
07-16 16:27:29.197: E/AndroidRuntime(11735): at lle.imagedownloader.LleUrlEnter$ImgDownloadTask.onPostExecute(LleUrlEnter.java:65)
07-16 16:27:29.197: E/AndroidRuntime(11735): at lle.imagedownloader.LleUrlEnter.onCreate(LleUrlEnter.java:23)
07-16 16:27:29.197: E/AndroidRuntime(11735): at android.app.Activity.performCreate(Activity.java:5058)
07-16 16:27:29.197: E/AndroidRuntime(11735): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-16 16:27:29.197: E/AndroidRuntime(11735): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-16 16:27:29.197: E/AndroidRuntime(11735): ... 11 more
答案 0 :(得分:0)
您正在错误地调用异步任务,在主线程上运行其代码。
替换
task.onPostExecute(task.doInBackground(imgLink));
与
task.execute(imgLink);
答案 1 :(得分:0)
对于遇到类似问题的任何人,请参阅this页面并确保添加
到应用程序的清单(在/ yourapplication / res /中找到)