问题
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.
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);
}
}
and this is my extended 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;
}
}
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.
Update:
In the activity I'm passing null as the cursor argument to the adapter constructor because:
- I don't know how to get a reference to the cursor since I'm using
getSupportLoaderManager().initLoader()
- the same thing is done on other SO threads due to the same reason and it seems to work fine for them
Update:
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
I would really appreciate any help. TIA
回答1:
first: get rid of this line:
private Cursor mCursor;
and instead of mCursor
use getCursor()
since you didnt override swapCursor
method and mCursor
will be point to the old cursor.
second: change
@Override
public int getCount() {return mCursor.getCount();}
to:
@Override
public int getCount() {
if(getCursor()==null)
return 0;
return getCursor().getCount();
}
or better do not override getCount
, getItem
and getItemId
at all
回答2:
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;
}
}
your problem is might because of you do not have initialized you object for Cursor and Context. try the above code. Might Help you.
回答3:
Needs to pass loader.loadInBackground() in
mAdapter = new ImageAdapter(getApplication(), R.layout.galleryitem,
loader.loadInBackground(), cols, views, 0);
来源:https://stackoverflow.com/questions/12069036/nullpointerexception-with-extended-simplecursoradapter