javaandroidxmlandroid-dialogfindviewbyid

Custom Dialog Error


I am having a difficult time displaying a Custom Dialog. I have also attempted to create it in a custom class that extends 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" />

Dialog creation

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();

            }
        });
    }

Log Display

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)

Solution

  • Your views belong to the dialog layout. So initialize using dialog.findViewById

    ImageView image = (ImageView)dialog.findViewById(R.id.image_display);
    

    Similarly the other views that belong to the dialog layout.

    Get rid of

    View layout = ((Activity) mContext).findViewById(R.layout.wish_dialog);
    

    findViewById looks for a view with the id mentioned in the current view hierarchy. You set the custom layout to the dialog. So use the dialog object to find views.