2

カスタムプロバイダーとローダーについて学んでいます。GridView簡単な例として、外部SDカードに保存されている画像を表示するを実装しようとしています。たくさん読んだことがありますが(ドキュメント、SOのスレッド、Googleグループ、フォーラムなど)、コードを機能させることができません。この例にはいくつかの問題が存在する可能性があることは知っていますが、段階的に進めていきたいと思います。コードを停止する最初のエラーはですNullPointerExceptionので、私の質問はそれを修正する方法です。

これは私の最小バージョンですActivity(サポートライブラリ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;
    }    
}

コードを実行すると、logcatはアダプタNullPointerExceptionのメソッドにを表示します。getCount()カーソルがアダプタに渡されていないようです。

更新

アクティビティでは、次の理由により、カーソル引数としてアダプタコンストラクタにnullを渡します。

  • 使用しているので、カーソルへの参照を取得する方法がわかりませんgetSupportLoaderManager().initLoader()
  • 同じ理由で他のSOスレッドでも同じことが行われ、それらのスレッドでは正常に機能しているようです。

更新

これはlogcatの出力です

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

助けていただければ幸いです。TIA

4

3 に答える 3

4

最初に: 次の行を取り除きます:

private Cursor mCursor;

メソッドをオーバーライド せず、古いカーソルを指すため、代わりにmCursor使用します。getCursor()swapCursormCursor

2番目: 変更

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

に:

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

またはオーバーライドしないでくださいgetCountgetItemgetItemId

于 2012-08-22T10:00:20.263 に答える
0
public class ImageAdapter extends SimpleCursorAdapter {
    private int columnIndex;

    private Context mContext;
    private Cursor mCursor;

    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;
    }    
}

あなたの問題は、カーソルとコンテキストのオブジェクトを初期化していないことが原因である可能性があります。上記のコードを試してください。あなたを助けるかもしれません。

于 2012-08-22T08:46:30.367 に答える
0

loader.loadInBackground() を渡す必要があります

  mAdapter = new ImageAdapter(getApplication(), R.layout.galleryitem,
                loader.loadInBackground(), cols, views, 0);
于 2013-10-29T22:00:54.187 に答える