Reputation: 317
I have a grid view of albums inside a viewpager frament. The idea is to display the contents of the album in a new window on clicking a grid item. Here is my code:
Inside onCreateView() method of Albums fragment:
GridView grid = (GridView) getView().findViewById(R.id.grid);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView al = (TextView) view.findViewById(R.id.album_name);
TextView ar = (TextView) view.findViewById(R.id.songs_num);
String album_name = (String) al.getText();
String artist_name = (String) ar.getText();
startActivity(new Intent(getActivity().getApplicationContext(), DisplayAlbumContent.class).putExtra("pos", position).putExtra("album_name", album_name).putExtra("artist_name", artist_name));
}
});
Here is DisplayAlbumContent.java :
public class DisplayAlbumContent extends Activity implements LoaderManager.LoaderCallbacks<Cursor>{
DisplaySongsAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.album_detail);
}
Intent i = getIntent();
Bundle b = i.getExtras();
String val[] = {(String) b.getCharSequence("album_name")};
static final String[] ALBUM_DETAIL_PROJECTION = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION};
String where = MediaStore.Audio.Media.ALBUM + "=?";
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String orderby = MediaStore.Audio.Media.TITLE;
return new CursorLoader(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,ALBUM_DETAIL_PROJECTION, where, val, orderby);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
However I am getting the following error:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.tekinarslan.material.sample/com.screens.DisplayAlbumContent}: java.lang.NullPointerException
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.screens.DisplayAlbumContent.<init>(DisplayAlbumContent.java:29)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1215)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 78
Reputation: 10328
Assuming you've posted an accurate version of your code, this is going to cause problems:
Intent i = getIntent();
Bundle b = i.getExtras();
String val[] = {(String) b.getCharSequence("album_name")};
These are declared as fields in your class, which means they will get initialized when the class instance is created. But when Android creates an instance of this class for you, it hasn't yet set the intent.
This means that i
will likely be getting the value null
from getIntent()
, causing the NPE to occur on the next line when i.getExtras()
is called.
You need to move the initialisation of these fields into the onCreate()
method. At that point, the Intent will have been set in your Activity instance.
public class DisplayAlbumContent extends Activity implements LoaderManager.LoaderCallbacks<Cursor>{
DisplaySongsAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.album_detail);
this.i = getIntent();
this.b = i.getExtras();
this.val = new String[] {(String) this.b.getCharSequence("album_name")};
}
Intent i;
Bundle b;
String[] val;
}
A few other notes:
i
's, b
's and val
s gets
confusing very quickly. []
next to the type, rather than next to the
variable - as I have done above (although they have the same effect).Upvotes: 1