在 Android 中单击 ImageView 时显示 FC 错误

Show FC error when click on ImageView in Android

我想在点击 ImageView 后在 imageview 中显示图像。我使用 Fragment 来显示内容,我需要在 Dialog 中显示大图。但是当点击 ImageView 时显示 FC 错误。

我的片段代码:

public class root_mobile_fragment extends Fragment {

    Bundle savedInstanceState;

    private CardView mobile_dl;
    private ImageView mobile_iamge1;
    Context context;

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.root_mobile_xml, container, false);

        mobile_dl = (CardView) view.findViewById(R.id.mobile_adApp);
        mobile_dl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://soft-mgyun-com.qcloudcdn.com/files/products/romastersu/2075/2016/70/RomasterSu_3.0.0_160104_2075.apk"));
                startActivity(browserIntent);
            }
        });

        mobile_iamge1 = (ImageView) view.findViewById(R.id.mobile_root_image1);
        mobile_iamge1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(getActivity());

                LayoutInflater inflater2 = LayoutInflater.from(getActivity());
                View newView = (View) inflater2.inflate(R.layout.image_dialog, null);

                ImageView image_2 = (ImageView) newView.findViewById(R.id.mobile_root_image1);
                image_2.setImageResource(R.drawable.iroot_1);

                dialog.show();
            }
        });

        return view;
    }
}

图片布局XML:

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

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mobile_image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:adjustViewBounds="true"
        android:background="@color/white"
        android:foreground="?android:attr/selectableItemBackground">

        <ImageView
            android:id="@+id/image_dialog_big"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true" />

    </android.support.v7.widget.CardView>

</RelativeLayout>

FC 错误:

01-15 15:08:09.174 16981-16981/com.aria_data_service.appsuite E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.aria_data_service.appsuite, PID: 16981
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
                                                                                    at com.aria_data_service.appsuite.fragments.root_mobile_fragment.onClick(root_mobile_fragment.java:56)
                                                                                    at android.view.View.performClick(View.java:4764)
                                                                                    at android.view.View$PerformClick.run(View.java:19844)
                                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5349)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

我该如何解决? tnx 全部 <3

我认为您在对话框中为 ImageView 使用了错误的 id

你有

    <ImageView
        android:id="@+id/image_dialog_big"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true" />

在您的 xml 中,但您在 java 代码中使用 R.id.mobile_root_image1 来访问 ImageView 并且它导致 NullPointerException 正如您在 logcat输出。 改变

ImageView image_2 = (ImageView)newView.findViewById(R.id.mobile_root_image1);

ImageView image_2 = (ImageView) newView.findViewById(R.id.image_dialog_big);

在您的 activity

中替换此行
mobile_iamge1 = (ImageView) view.findViewById(R.id.mobile_root_image1);

mobile_iamge1 = (ImageView) view.findViewById(R.id.image_dialog_big);

用它来显示对话框

在你的 onclick 中试试这个

final Dialog dialog = new Dialog(getActivity());

            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.image_dialog);
            ImageView image_2 =(ImageView) dialog.findViewById(R.id.mobile_root_image1);

            image_2.setImageResource(R.drawable.iroot_1);


            });
            dialog.show();

并确保 image_dialog xml 文件包含 mobile_root_image1 作为图像视图的 ID