单击android时在对话框中显示imageView

时间:2015-08-12 21:12:56

标签: android imageview android-alertdialog

我有一个ImageView,其数据集来自java。

我的问题是:当我点击该ImageView时,如何在Dialog中显示此图像?

我写了一些代码,但有NullPointerException

final ImageView imageView = (ImageView)view.findViewById(R.id.profile_img);
imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Dialog settingsDialog = new Dialog(getActivity());
                    ImageView iv= (ImageView) view.findViewById(R.id.profile_img_popup);
                    Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
                    iv.setImageBitmap(bm);
                    settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                    settingsDialog.setContentView(getActivity().getLayoutInflater().inflate(R.layout.image_layout
                            , null));
                    settingsDialog.show();


                }
            });

image_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/profile_img_popup"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="dismissListener"
        android:text="OK"/>
</LinearLayout>

LOG cat is
08-13 01:46:46.386    2583-2583/com.saniyeh.iwedding E/cutils﹕ to chown(/mnt/shell/emulated/0, 0, 0)
08-13 01:46:46.406    2583-2583/com.saniyeh.iwedding E/cutils﹕ to chown(/mnt/shell/emulated/obb, 0, 0)
08-13 01:46:46.406    2583-2583/com.saniyeh.iwedding E/cutils﹕ to chown(/storage/emulated/0/Android, 0, 0)
08-13 01:46:46.408    2583-2583/com.saniyeh.iwedding E/cutils﹕ to chown(/storage/emulated/0/Android/obb, 0, 0)
08-13 01:46:50.560    2583-2583/com.saniyeh.iwedding E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.saniyeh.iwedding.FragmentProfile$1.onClick(FragmentProfile.java:154)
            at android.view.View.performClick(View.java:4211)
            at android.view.View$PerformClick.run(View.java:17446)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

试试这个

imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Dialog settingsDialog = new Dialog(getActivity());

        LayoutInflater inflater = getLayoutInflater();
        View newView = (View) inflater.inflate(R.layout.image_layout, null);

        settingsDialog.setContentView(newView);
        settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        ImageView iv= (ImageView) newView.findViewById(R.id.profile_img_popup);
        Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
        iv.setImageBitmap(bm);


        settingsDialog.show();


    }
});

答案 1 :(得分:0)

我在java代码中做了一些更改然后问题解决了:

imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Dialog settingsDialog = new Dialog(getActivity());

                    View v = getActivity().getLayoutInflater().inflate(R.layout.image_layout, null);
                    ImageView iv = (ImageView) v.findViewById(R.id.profile_img_popup);
                    Bitmap bm = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                    iv.setImageBitmap(bm);
                    settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                    settingsDialog.setContentView(v);
                    settingsDialog.show();


                }
            });