我的应用程序在执行时崩溃我已经使用位图array
来存储图像。可以任何人帮我特别感谢提前不提供网址,但我使用的是有效的图片网址,所以没有问题。
package abc.expandable;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String URL =
" some url";
public static final String URL1="some url";
ImageView imageView;
ImageView imageView1;
ArrayList<Bitmap> bitmapArray=new ArrayList<Bitmap>(4);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
imageView1=(ImageView)findViewById(R.id.imageView1);
// Create an object for subclass of AsyncTask
GetXMLTask task = new GetXMLTask();
// Execute the task
int a=0;
task.execute(new String[]{URL});
imageView.setImageBitmap(bitmapArray.get(0));
Toast.makeText(MainActivity.this, "downloading image please wait ", Toast.LENGTH_SHORT).show();
// task.execute(new String[]{URL1});
//imageView1.setImageBitmap(bitmapArray.get(a));
}
private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
}
return map;
}
// Sets the Bitmap returned by doInBackground
@Override
protected void onPostExecute(Bitmap result)
{
// imageView.setImageBitmap(bitmapArray.get(0));
Toast.makeText(MainActivity.this, "downloaded", Toast.LENGTH_SHORT).show();
Log.i("bitmap", "bitmap ");
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.
decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
bitmapArray.add(bitmap);
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
}
这是log cat
03-04 18:59:24.307 18588-18588/abc.expandable E/AndroidRuntime: FATAL EXCEPTION: main
Process: abc.expandable, PID: 18588
java.lang.RuntimeException: Unable to start activity ComponentInfo{abc.expandable/abc.expandable.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at abc.expandable.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
答案 0 :(得分:0)
删除此行,它应该有效:
sample_matrix<-t(sample_matrix)
dummy_func1 <- function(x) c(1, 2)
a <- lapply(as.data.frame(sample_matrix) , dummy_func1)
t(data.frame(a))
[,1] [,2]
v1 1 2
v2 1 2
v3 1 2
dummy_func2 <- function(x) c(1)
b<- lapply(as.data.frame(sample_matrix), dummy_func2)
t(data.frame(b))
[,1]
v1 1
v2 1
v3 1
您正在访问一个空数组,这会导致异常,因为您的数组成员被添加到比您的UI线程慢的其他线程中。您应该在ASyncTask的onPostExecute中设置imageview。这样,您可以确保ASyncTask已完成,并在尝试访问它们之前已将成员添加到阵列中。