我很难显示自定义对话框。我还尝试在扩展Dialog的自定义类中创建它。
Dialog XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- android:background="@drawable/image_bg" -->
<ImageView
android:id="@+id/image_display"
android:layout_width="250dip"
android:layout_height="250dip" />
<TextView
android:id="@+id/title_display"
style="@android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:padding="5dp" />
</LinearLayout>
<TextView
android:id="@+id/desc_display"
style="@android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:visibility="gone" />
<TextView
android:id="@+id/url_display"
style="@android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:visibility="gone" />
<TextView
android:id="@+id/loc_display"
style="@android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:visibility="gone" />
<RatingBar
android:id="@+id/priority_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:isIndicator="true"
android:numStars="5"
android:padding="5dp"
android:stepSize="1.0"
android:rating="2.0"
android:visibility="gone" />
<TextView
android:id="@+id/price_display"
style="@android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:visibility="gone" />
<TextView
android:id="@+id/ean_display"
style="@android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:visibility="gone" />
<TextView
android:id="@+id/date_display"
style="@android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:visibility="gone" />
<Button
android:id="@+id/button_display"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OK" />
对话创建
protected void createDialog(Wish w)
{
// custom dialog
final Dialog dialog = new Dialog((Activity) mContext);
View layout = ((Activity) mContext).findViewById(R.layout.wish_dialog);
dialog.setContentView(R.layout.wish_dialog);
dialog.setTitle("Wish");
ImageView image = (ImageView) ((Activity) mContext).findViewById(R.id.image_display);
TextView title = (TextView) ((Activity) mContext).findViewById(R.id.title_display);
TextView desc = (TextView) ((Activity) mContext).findViewById(R.id.desc_display);
TextView loc = (TextView) ((Activity) mContext).findViewById(R.id.loc_display);
TextView url = (TextView) ((Activity) mContext).findViewById(R.id.url_display);
RatingBar rating = (RatingBar) ((Activity) mContext).findViewById(R.id.priority_display);
TextView price = (TextView) ((Activity) mContext).findViewById(R.id.price_display);
TextView barcode = (TextView) ((Activity) mContext).findViewById(R.id.ean_display);
TextView date = (TextView) ((Activity) mContext).findViewById(R.id.date_display);
Button button = (Button) ((Activity) mContext).findViewById(R.id.button_display);
title.setText(w.getTitle().toUpperCase());
title.setTypeface(null, Typeface.BOLD);
if (w.getImage() != null)
{
image.setImageBitmap(decodeBitmap(w.getImage(), image.getWidth(), image.getHeight()));
}
else
{
Drawable drawable = mContext.getResources().getDrawable(
mContext.getResources().getIdentifier("image_preview", "drawable",
mContext.getPackageName()));
image.setImageDrawable(drawable);
}
if (w.getDesc() != null)
{
desc.setVisibility(View.VISIBLE);
desc.setText(w.getDesc());
}
if (w.getLocation() != null)
{
loc.setVisibility(View.VISIBLE);
loc.setText(w.getLocation());
}
else if (w.getUrl() != null)
{
url.setVisibility(View.VISIBLE);
url.setText(w.getUrl());
}
if(w.getPriority() != 0)
{
rating.setVisibility(View.VISIBLE);
rating.setRating(w.getPriority());
}
dialog.show();
Log.i("Click List", "Show");
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dialog.dismiss();
}
});
}
日志显示
04-10 10:27:26.588: E/AndroidRuntime(12035): FATAL EXCEPTION: main
04-10 10:27:26.588: E/AndroidRuntime(12035): java.lang.NullPointerException
04-10 10:27:26.588: E/AndroidRuntime(12035): at com.example.youwish.WishAdapter.createDialog(WishAdapter.java:167)
04-10 10:27:26.588: E/AndroidRuntime(12035): at com.example.youwish.WishAdapter$1.onClick(WishAdapter.java:72)
04-10 10:27:26.588: E/AndroidRuntime(12035): at android.view.View.performClick(View.java:4475)
04-10 10:27:26.588: E/AndroidRuntime(12035): at android.view.View$PerformClick.run(View.java:18786)
04-10 10:27:26.588: E/AndroidRuntime(12035): at android.os.Handler.handleCallback(Handler.java:730)
04-10 10:27:26.588: E/AndroidRuntime(12035): at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 10:27:26.588: E/AndroidRuntime(12035): at android.os.Looper.loop(Looper.java:176)
04-10 10:27:26.588: E/AndroidRuntime(12035): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-10 10:27:26.588: E/AndroidRuntime(12035): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 10:27:26.588: E/AndroidRuntime(12035): at java.lang.reflect.Method.invoke(Method.java:525)
04-10 10:27:26.588: E/AndroidRuntime(12035): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
04-10 10:27:26.588: E/AndroidRuntime(12035): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-10 10:27:26.588: E/AndroidRuntime(12035): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您的视图属于对话框布局。所以使用dialog.findViewById
ImageView image = (ImageView)dialog.findViewById(R.id.image_display);
与属于对话框布局的其他视图类似。
摆脱
View layout = ((Activity) mContext).findViewById(R.layout.wish_dialog);
findViewById
查找具有当前视图层次结构中提到的id的视图。您将自定义布局设置为对话框。因此,使用对话框对象来查找视图。
答案 1 :(得分:1)
试试这个..
更改beloe代码
ImageView image = (ImageView) ((Activity) mContext).findViewById(R.id.image_display);
TextView title = (TextView) ((Activity) mContext).findViewById(R.id.title_display);
TextView desc = (TextView) ((Activity) mContext).findViewById(R.id.desc_display);
TextView loc = (TextView) ((Activity) mContext).findViewById(R.id.loc_display);
TextView url = (TextView) ((Activity) mContext).findViewById(R.id.url_display);
RatingBar rating = (RatingBar) ((Activity) mContext).findViewById(R.id.priority_display);
TextView price = (TextView) ((Activity) mContext).findViewById(R.id.price_display);
TextView barcode = (TextView) ((Activity) mContext).findViewById(R.id.ean_display);
TextView date = (TextView) ((Activity) mContext).findViewById(R.id.date_display);
Button button = (Button) ((Activity) mContext).findViewById(R.id.button_display);
作为
ImageView image = (ImageView) dialog.findViewById(R.id.image_display);
TextView title = (TextView) dialog.findViewById(R.id.title_display);
TextView desc = (TextView) dialog.findViewById(R.id.desc_display);
TextView loc = (TextView) dialog.findViewById(R.id.loc_display);
TextView url = (TextView) dialog.findViewById(R.id.url_display);
RatingBar rating = (RatingBar) dialog.findViewById(R.id.priority_display);
TextView price = (TextView) dialog.findViewById(R.id.price_display);
TextView barcode = (TextView) dialog.findViewById(R.id.ean_display);
TextView date = (TextView) dialog.findViewById(R.id.date_display);
Button button = (Button) dialog.findViewById(R.id.button_display);
并删除
View layout = ((Activity) mContext).findViewById(R.layout.wish_dialog);
答案 2 :(得分:0)
在您的代码中,您已单独对视图进行了充气,并在对话框中明确设置了布局。因此,您在代码中没有使用View layout
,因为您明确设置了布局dialog.setContentView(R.layout.wish_dialog);
,而它应该像dialog.setContentView(layout);
您需要更改以下代码:
// custom dialog
final Dialog dialog = new Dialog((Activity) mContext);
View layout = ((Activity) mContext).findViewById(R.layout.wish_dialog);
dialog.setContentView(R.layout.wish_dialog);
dialog.setTitle("Wish");
ImageView image = (ImageView) ((Activity) mContext).findViewById(R.id.image_display);
TextView title = (TextView) ((Activity) mContext).findViewById(R.id.title_display);
TextView desc = (TextView) ((Activity) mContext).findViewById(R.id.desc_display);
TextView loc = (TextView) ((Activity) mContext).findViewById(R.id.loc_display);
..................................
更改上面的代码如下:
// custom dialog
final Dialog dialog = new Dialog((Activity) mContext);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = mInflater.inflate(R.layout.wish_dialog);
dialog.setContentView(layout);
dialog.setTitle("Wish");
ImageView image = (ImageView)layout.findViewById(R.id.image_display);
TextView title = (TextView) layout .findViewById(R.id.title_display);
TextView desc = (TextView) layout .findViewById(R.id.desc_display);
TextView loc = (TextView) layout .findViewById(R.id.loc_display);
TextView url = (TextView) layout .findViewById(R.id.url_display);
RatingBar rating = (RatingBar)layout .findViewById(R.id.priority_display);
TextView price = (TextView)layout .findViewById(R.id.price_display);
TextView barcode = (TextView)layout .findViewById(R.id.ean_display);
TextView date = (TextView) layout .findViewById(R.id.date_display);
Button button = (Button) layout .findViewById(R.id.button_display