我调用活动ChunkedImageActivity后,我的应用程序崩溃了。我只看到一个黑屏。有人告诉我,问题在于创建arraylist。
我的arraylist应该包含一个ImageView片段,在其上显示来自R.drawable
的图像。
以下是此活动调用的第二个Activity
和BaseAdapter
的代码。
你能否告诉我创建arraylist是否存在问题?
ChunckedImageActivity
public class ChunkedImageActivity extends Activity {
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.image_grid);
//Getting the image chunks sent from the previous activity
ArrayList<Bitmap> imageChunks = getIntent().getParcelableArrayListExtra("image
chunks");
//Getting the grid view and setting an adapter to it
GridView grid = (GridView) findViewById(R.id.gridview);
grid.setAdapter(new ImageAdapter(this, imageChunks));
grid.setNumColumns((int) Math.sqrt(imageChunks.size()));
}
}
ImageAdapter
package com.example.laptop.gridsplitter;
import java.util.ArrayList;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
//The adapter class associated with the ChunkedImageActivity class
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Bitmap> imageChunks;
private int imageWidth, imageHeight;
//constructor
public ImageAdapter(Context c, ArrayList<Bitmap> images){
mContext = c;
imageChunks = images;
imageWidth = images.get(0).getWidth();
imageHeight = images.get(0).getHeight();
}
@Override
public int getCount() {
return imageChunks.size();
}
@Override
public Object getItem(int position) {
return imageChunks.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image;
if(convertView == null){
image = new ImageView(mContext);
/*
* NOTE: I have set imageWidth - 10 and imageHeight
* as arguments to LayoutParams class.
* But you can take anything as per your requirement
*/
image.setLayoutParams(new GridView.LayoutParams(imageWidth - 10 , imageHeight));
image.setPadding(0, 0, 0, 0);
}else{
image = (ImageView) convertView;
}
image.setImageBitmap(imageChunks.get(position));
return image;
}
}
logcat的
<1> main@830025864456, prio=5, in group 'main', status: 'RUNNING'
at com.example.laptop.gridsplitter.ImageActivity.onClick(ImageActivity.java:68)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17184)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Method.java:-1)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(NativeStart.java:-1)
<11> Binder_3@830036764080, prio=5, in group 'main', status: 'RUNNING'
at dalvik.system.NativeStart.run(NativeStart.java:-1)
<10> Binder_2@830036679432, prio=5, in group 'main', status: 'RUNNING'
at dalvik.system.NativeStart.run(NativeStart.java:-1)
<9> Binder_1@830036678520, prio=5, in group 'main', status: 'RUNNING'
at dalvik.system.NativeStart.run(NativeStart.java:-1)
<8> FinalizerWatchdogDaemon@830036661992 daemon, prio=5, in group 'system', status:
'WAIT'
at java.lang.Object.wait(Object.java:-1)
at java.lang.Object.wait(Object.java:364)
at java.lang.Daemons$FinalizerWatchdogDaemon.run(Daemons.java:214)
at java.lang.Thread.run(Thread.java:856)
<7> FinalizerDaemon@830036661560 daemon, prio=5, in group 'system', status: 'WAIT'
at java.lang.Object.wait(Object.java:-1)
at java.lang.Object.wait(Object.java:401)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:102)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:73)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:169)
at java.lang.Thread.run(Thread.java:856)
<6> ReferenceQueueDaemon@830036661200 daemon, prio=5, in group 'system', status: 'WAIT'
at java.lang.Object.wait(Object.java:-1)
at java.lang.Object.wait(Object.java:364)
at java.lang.Daemons$ReferenceQueueDaemon.run(Daemons.java:129)
at java.lang.Thread.run(Thread.java:856)
<5> Compiler@830036660960 daemon, prio=5, in group 'system', status: 'WAIT'
at dalvik.system.NativeStart.run(NativeStart.java:-1)
<3> Signal Catcher@830036660480 daemon, prio=5, in group 'system', status: 'WAIT'
at dalvik.system.NativeStart.run(NativeStart.java:-1)
<2> GC@830036660256 daemon, prio=5, in group 'system', status: 'WAIT'
at dalvik.system.NativeStart.run(NativeStart.java:-1)