GridView setAdapter方法的NullPointerException

时间:2014-05-21 22:07:37

标签: android gridview simplecursoradapter

当我尝试将SimpleCursorAdapter的实例设置为GridView的适配器时,我得到NullPointerException

E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sp.sr.app/com.sp.sr.app.GridActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
        at android.app.ActivityThread.access$600(ActivityThread.java:150)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:5225)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.sp.sr.app.GridActivity.onCreate(GridActivity.java:36)
        at android.app.Activity.performCreate(Activity.java:5133)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
        at android.app.ActivityThread.access$600(ActivityThread.java:150)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:5225)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        at dalvik.system.NativeStart.main(Native Method)

这是我的GridAdapter类

package com.sp.sr.app;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class GridAdapter extends SimpleCursorAdapter {
    private static final String TAG = GridAdapter.class.getSimpleName();

    private Context context;
    private int layout;
    private Cursor cursor;
    private LayoutInflater inflater;

    private final int STRING_TITLE;
    private final int STRING_DESCRIPTION;
    private final int STRING_COVER;

    public GridAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
        super(context, layout, cursor, from, to, 0);

        this.context = context;
        this.layout = layout;
        this.cursor = cursor;
        this.inflater = LayoutInflater.from(context);

        this.STRING_TITLE = cursor.getColumnIndex(StringContract.Column.TITLE);
        this.STRING_DESCRIPTION = cursor.getColumnIndex(StringContract.Column.DESCRIPTION);
        this.STRING_COVER = cursor.getColumnIndex(StringContract.Column.COVER);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (cursor.moveToPosition(position)) {
            ViewHolder viewHolder;

            if (convertView == null) {
                convertView = inflater.inflate(layout, null);

                viewHolder = new ViewHolder();
                viewHolder.string_title = (TextView) convertView.findViewById(R.id.string_title);
                viewHolder.string_description = (TextView) convertView.findViewById(R.id.string_description);
                viewHolder.string_cover = (ImageView) convertView.findViewById(R.id.string_cover);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.string_title.setText(cursor.getString(STRING_TITLE));
                viewHolder.string_description.setText(cursor.getString(STRING_DESCRIPTION));
                Picasso.with(context).load(cursor.getString(STRING_COVER)).into(viewHolder.string_cover);
        }

        return convertView;
    }

    private class ViewHolder {
        ImageView string_cover;
        TextView string_title;
        TextView string_description;
    }
}

这是我活动的onCreate方法

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Cursor cursor = getContentResolver().query(StringContract.CONTENT_URI, FROM, null, null, StringContract.DEFAULT_SORT);
        startManagingCursor(cursor);

        mAdapter = new GridAdapter(getApplicationContext(), R.layout.list_item, cursor, FROM, TO);

        grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(mAdapter); // Line 36 of GridActivity.java
    }

提前感谢您的帮助

1 个答案:

答案 0 :(得分:3)

grid var返回null,因为系统无法在此行找到其ID:

grid = (GridView) findViewById(R.id.grid); // id not found

确实,您没有将布局附加到Activity,这就是为什么当您尝试设置Adapter时,将其设置为null变量:

grid.setAdapter(mAdapter); // grid = null, the system cannot do that -> CRASH 

然后,您需要使用Activity方法将布局附加到setContentView,以便对系统说:此视图属于通过findViewById方法检索的此布局:

super.onCreate(savedInstanceState); // always below 'super' method
setContentView(R.layout.grid_layout); // attach the layout
                                      // then, find the views regarding it
grid = (GridView) findViewById(R.id.grid); // id found on this layout