如何使用SDCard在gridview中设置图像

时间:2011-07-12 11:02:38

标签: android

我想在SDCard的网格视图中设置图像。我使用静态图像制作了gridview。但是当我运行我的项目时SDCard显示了nullpointer的错误。

public class MyGridViewSDActivity extends Activity{
    private Cursor cursor;
    private int columnIndex;
    ImageAdapter adapter;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mygridacti);

        String[] projection = {MediaStore.Images.Thumbnails._ID};
        cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Thumbnails._ID);
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

        GridView gView = (GridView)findViewById(R.id.grdvw);
        gView.setAdapter(new ImageAdapter(this));
        adapter.notifyDataSetChanged();

        gView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) {
                // TODO Auto-generated method stub
                String[] projection = {MediaStore.Images.Thumbnails.DATA};
                cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Thumbnails.IMAGE_ID);
                columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
                cursor.moveToPosition(position);
                String imagePath = cursor.getString(columnIndex);
            }
        });
    }
}

class ImageAdapter extends BaseAdapter{
    Cursor cursor;
    ImageView imageview;
    Context mContext;
    int columnIndex;
    public ImageAdapter(Context adapter){
        mContext = adapter;
    }

    public ImageAdapter(OnClickListener onClickListener) {
        // TODO Auto-generated constructor stub

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return cursor.getCount();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(convertView == null){
            imageview = new ImageView(mContext);
            cursor.moveToPosition(position);
            int imageID = cursor.getInt(columnIndex);
            imageview.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+imageID));
            imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageview.setPadding(8, 8, 8, 8);
            imageview.setLayoutParams(new GridView.LayoutParams(100, 100));
        }
        else{
            imageview = (ImageView)convertView;
        }
        return imageview;
    }
}

错误:

07-12 12:07:30.449: ERROR/AndroidRuntime(2231): FATAL EXCEPTION: main
07-12 12:07:30.449: ERROR/AndroidRuntime(2231): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myworkspace/com.myworkspace.MyGridViewSDActivity}: java.lang.NullPointerException
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at android.os.Looper.loop(Looper.java:123)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at android.app.ActivityThread.main(ActivityThread.java:3647)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at java.lang.reflect.Method.invokeNative(Native Method)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at java.lang.reflect.Method.invoke(Method.java:507)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at dalvik.system.NativeStart.main(Native Method)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231): Caused by: java.lang.NullPointerException
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at com.myworkspace.ImageAdapter.getCount(MyGridViewSDActivity.java:64)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at android.widget.GridView.setAdapter(GridView.java:131)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at com.myworkspace.MyGridViewSDActivity.onCreate(MyGridViewSDActivity.java:31)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
07-12 12:07:30.449: ERROR/AndroidRuntime(2231):     ... 11 more

1 个答案:

答案 0 :(得分:0)

空指针异常似乎来自return cursor.getCount();函数中代码public int getCount()的这一部分。

您尚未初始化游标对象,但您正在取消引用它,因此异常。 Soln - 在解除引用之前将光标初始化为某些内容(除了以外的任何内容:))。