Android的自定义对话框的NullPointerException [英] Android Custom Dialog NullPointerException

查看:265
本文介绍了Android的自定义对话框的NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能为我的生活弄清楚为什么我得到一个NullPointerException异常。

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设定为= 0

DIALOG_COVER is set to = 0.

相关onCreateDialog看起来是这样的:

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

作为参考,这是cover_dialog.xml:

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>

现在,pviously描述的图像$ P $被点击时,应用程序立即崩溃,并通过LogCat中引发以下错误:

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)

问题的线是指这条线内onCreateDialog的:

The line in question refers to this line inside of onCreateDialog:

coverLarge.setImageBitmap(book.getCover());

基本上,我不知道为什么coverLarge是在这一点空。任何帮助将是非常美联社preciated。

Basically, I don't get why coverLarge is null at that point. Any help would be much appreciated.

推荐答案

什么是关于:

/** 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

这篇关于Android的自定义对话框的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆