nterException异常延长SimpleCursorAda

nterException异常延长SimpleCursorAda

本文介绍了NullPointerException异常延长SimpleCursorAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关自定义提供商和装载机。举一个简单的例子,我想实现一个的GridView 显示存储在外部SD卡中的图片。虽然我已经读了很多(在SO文件,线程,谷歌群,论坛......),我不能让我的code工作。我知道的几个问题可能会在$此如对$ psent但我要一步一步来。第一个错误停止code是一个 NullPointerException异常,所以我的问题是如何解决它。

I'm learning about custom providers and loaders. As a simple example I'm trying to implement a GridView that shows the pictures stored in the external SD card. Although I've read a lot (documentation, threads on SO, Google groups, forums...) I'm not able to get my code working. I know that several issues may be present on this example but I want to go step by step. The first error stopping the code is a NullPointerException, so my question is how to fix it.

这是我的活动的最小版本(它使用支持库V4):

This is a minimal version of my Activity (it uses support library v4):

public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
    private String[] columns = {
            MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID
    };
    private String orderBy = MediaStore.Images.Media._ID;
    private ImageAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Prepare the loader
        getSupportLoaderManager().initLoader(0, null, this);
        // Initialize the layout Adapter
        String[] cols = new String[] {MediaStore.Images.Media.DATA};
        int[] views = new int[] {R.id.thumbImage};
        mAdapter = new ImageAdapter(getApplication(), R.layout.galleryitem,
                null, cols, views, 0);
        // Set the layout Adapter
        GridView gridview = (GridView) findViewById(R.id.gallery_gridview);
        gridview.setAdapter(mAdapter);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Uri baseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        return new CursorLoader(getApplication(), baseUri, columns,
                null, null, orderBy);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader){
        mAdapter.swapCursor(null);
    }
}

这是我的延长 SimpleCursorAdapter

public class ImageAdapter extends SimpleCursorAdapter {
    private int columnIndex;
    private LayoutInflater mInflater;
    private class ViewHolder {
        ImageView imageView;
    }

    public ImageAdapter(Context c, int layout, Cursor aCursor, String[] from,
            int[] to, int flags) {
        super(c, layout, aCursor, from, to, flags);
        this.mContext = c;
        this.mCursor = aCursor;
        mInflater = (LayoutInflater)
                c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {return mCursor.getCount();}

    @Override
    public Object getItem(int position) {return position;}

    @Override
    public long getItemId(int position) {return position;}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        View cell = convertView;
        if (cell == null) {
            vh = new ViewHolder();
            // No View passed, create one.
            cell = mInflater.inflate(R.layout.galleryitem, null);
            // Populate the ViewHolder
            vh.imageView = (ImageView) cell.findViewById(R.id.thumbImage);
            // Store the ViewHolder inside the layout
            cell.setTag(vh);
            // Setup the View behavior by setting some listeners...
        } else {
            vh = (ViewHolder) cell.getTag();
        }
        // Update the cell View state
        // Move the cursor to the current position
        mCursor.moveToPosition(position);
        // Get the current value for the requested position
        columnIndex = mCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        int imageID = mCursor.getInt(columnIndex);
        // Set the content of the image based on the provided Uri
        vh.imageView.setImageURI(Uri.withAppendedPath(
                MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" +
        imageID));
        return cell;
    }
}

当我运行code中的logcat中显示了一个 NullPointerException异常 getCount将()方法适配器。似乎光标不被传递给适配器

When I run the code the logcat shows a NullPointerException at the getCount() method of the adapter. It seems that the cursor is not being passed to the adapter.

更新

在活动我传递null作为指针参数到适配器构造函数,因为:

In the activity I'm passing null as the cursor argument to the adapter constructor because:

  • 在我不知道如何,因为我使用 getSupportLoaderManager()来得到一个参考光标。initLoader()
  • 同样的事情做对其他SO线程,由于同样的原因,它似乎好工作为他们

更新

这是logcat的输出

This is the logcat output

08-22 09:19:44.754: I/Process(5758): Sending signal. PID: 5758 SIG: 9
08-22 09:23:16.041: D/AndroidRuntime(8691): Shutting down VM
08-22 09:23:16.041: W/dalvikvm(8691): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
08-22 09:23:16.041: E/AndroidRuntime(8691): Uncaught handler: thread main exiting due to uncaught exception
08-22 09:23:16.061: E/AndroidRuntime(8691): java.lang.RuntimeException: Unable to start activity ComponentInfo{uvesoft.com.mycustomadapter/uvesoft.com.mycustomadapter.MainActivity}: java.lang.NullPointerException
08-22 09:23:16.061: E/AndroidRuntime(8691):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at android.os.Looper.loop(Looper.java:123)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at java.lang.reflect.Method.invokeNative(Native Method)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at java.lang.reflect.Method.invoke(Method.java:521)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at dalvik.system.NativeStart.main(Native Method)
08-22 09:23:16.061: E/AndroidRuntime(8691): Caused by: java.lang.NullPointerException
08-22 09:23:16.061: E/AndroidRuntime(8691):     at uvesoft.com.mycustomadapter.ImageAdapter.getCount(ImageAdapter.java:34)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at android.widget.GridView.setAdapter(GridView.java:128)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at uvesoft.com.mycustomadapter.MainActivity.onCreate(MainActivity.java:37)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-22 09:23:16.061: E/AndroidRuntime(8691):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-22 09:23:16.061: E/AndroidRuntime(8691):     ... 11 more
08-22 09:23:16.081: I/dalvikvm(8691): threadid=7: reacting to signal 3
08-22 09:23:16.081: E/dalvikvm(8691): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

我真的AP preciate任何帮助。 TIA

I would really appreciate any help. TIA

推荐答案

第一:摆脱这一行:

private Cursor mCursor;

和代替 mCursor 使用 getCursor(),因为你没有覆盖 swapCursor 方法和 mCursor 将指向旧光标。

and instead of mCursor use getCursor() since you didnt override swapCursor method and mCursor will be point to the old cursor.

第二:变化

@Override
    public int getCount() {return mCursor.getCount();}

@Override
public int getCount() {
  if(getCursor()==null)
     return 0;
  return getCursor().getCount();
}

或最好不要覆盖 getCount将的getItem getItemId 所有

这篇关于NullPointerException异常延长SimpleCursorAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:07