Android Custom Dialog NullPointerException
- by Kyle Hughes
I cannot for the life of me figure out why I'm getting a NullPointerException.
When a user clicks on a particular image, a dialog window is supposed to pop-up and display a larger version of said image:
private OnClickListener coverListener = new OnClickListener()
{
public void onClick(View v)
{
showDialog(DIALOG_COVER);
}
};
DIALOG_COVER is set to = 0.
The associated onCreateDialog looks like this:
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id)
{
case DIALOG_COVER:
dialog = new Dialog(mContext);
dialog.setContentView(R.layout.cover_dialog);
dialog.setTitle(book.getTitle());
ImageView coverLarge = (ImageView)findViewById(R.id.coverLarge);
coverLarge.setImageBitmap(book.getCover());
break;
default:
dialog = null;
}
return dialog;
}
For reference, this is cover_dialog.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/coverDialog"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="@+id/coverLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
/></LinearLayout>
Now, when the image previously described is clicked, the application immediately crashes and throws the following error through LogCat:
06-08 13:29:17.727: ERROR/AndroidRuntime(2220): Uncaught handler: thread main exiting due to uncaught exception
06-08 13:29:17.757: ERROR/AndroidRuntime(2220): java.lang.NullPointerException
06-08 13:29:17.757: ERROR/AndroidRuntime(2220): at org.kylehughes.android.brarian.AndroidBrarian.onCreateDialog(AndroidBrarian.java:259)
The line in question refers to this line inside of onCreateDialog:
coverLarge.setImageBitmap(book.getCover());
Basically, I don't get why coverLarge is null at that point. Any help would be much appreciated.