我不能爲我的生活找出爲什麼我得到一個NullPointerException。Android自定義對話框空指針異常
當用戶點擊特定圖像上,一個對話框窗口應該彈出並顯示所述圖像的放大版本:
private OnClickListener coverListener = new OnClickListener()
{
public void onClick(View v)
{
showDialog(DIALOG_COVER);
}
};
DIALOG_COVER被設定爲= 0。
的相關onCreateDialog看起來是這樣的:
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:
<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>
現在,先前所描述的圖像被點擊時,該應用程序立即崩潰和通過的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的:
coverLarge.setImageBitmap(book.getCover());
基本上,我不明白爲什麼coverLarge在這一點上是空的。任何幫助將非常感激。
基本上,我正在尋找對話框視圖中的R.id.coverLarge引用,同時您正在將它查找到活動視圖 – 2010-06-08 19:22:54
您剛剛爲我節省了更多的小時數。非常感謝你。 – 2010-06-08 19:25:48