我是Java的新手,已经分配了一项任务,通过JSOUP将图像实现到imageview中。
我能够解析图像网址,但问题是每当我运行应用程序时(在分配BITMAP之后)它崩溃了。
ArrayList<String> name= new ArrayList<String>();
ArrayList<String> url= new ArrayList<String>();
int chosenOne = 0;
ImageView mainImg;
public class getImageFile extends AsyncTask<String, Void, Bitmap>{
@Override
protected Bitmap doInBackground(String... urls) {
try {
URL url = new URL((urls[0]));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inStream = connection.getInputStream();
Bitmap image = BitmapFactory.decodeStream(inStream);
return image;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
getImageFile getImage = new getImageFile();
Bitmap image1= null;
try {
image1 = getImage.execute(image.get(chosenOne)).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
mainImg.setImageBitmap(image1);
我现在一直在崩溃,问题可能在这里?
记录CAT错误:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fun, PID: 2363
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aakarshm.fun.MainActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.fun.artistguessgame.MainActivity.onCreate(MainActivity.java:159)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/art: Suspending all threads took: 7.818ms