Android Custom Dialog NullPointerException

2019-06-03 06:53发布

问题:

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.

回答1:

What's about:

/** snip **/
LayoutInflater factory = LayoutInflater.from(mContext);
View dialogView = factory.inflate(R.layout.cover_dialog,null);
ImageView coverLarge = (ImageView)dialogView.findViewById(R.id.coverLarge);
dialog = new Dialog(mContext);
dialog.setContentView(dialogView);
dialog.setTitle(book.getTitle());
coverLarge.setImageBitmap(book.getCover());
/** snip **/

Just written from scratch. Please check the syntax



回答2:

This

(ImageView)findViewById(R.id.coverLarge);

returns a nullvalue. You may be passing an invalid/malformed id String to the findViewById method. Have you checked (debugged) what this String looks like before you get the NPE?



回答3:

Is it possible that you have a different xml file defining the same ID (coverLarge) for a different kind of View (say a Button)? Also, notice how the id you use to setup the dialog is cover_dialog but in the XML file you have coverDialog



回答4:

You have a few things going on at that line that some debug output would help. I'd start by System.out.println'ing the values of coverLarge and book immediately before the offending line. I know they shouldn't be null, but it wouldn't hurt to rule those possibilities out.