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

时间:2016-01-15 11:39:08

标签: android android-fragments android-view

点击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$2.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 all&lt; 3

3 个答案:

答案 0 :(得分:0)

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

你有

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

答案 1 :(得分:0)

在活动中替换此行

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

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

答案 2 :(得分:0)

使用此按钮显示对话框

在你的onclick中尝试这个

    imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
    imageView.backgroundColor = .greenColor()
    imageView.image = UIImage(contentsOfFile: document.documentImageURL)
    imageView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] //


   func setZoomScale() {
    let widthRatio = imageView.bounds.size.width / (imageView.image?.size.width)!
    let heightRatio = imageView.bounds.size.height / (imageView.image?.size.height)!
    let scale = min(widthRatio, heightRatio)
    let imageWidth = scale * (imageView.image?.size.width)!
    let imageHeight = scale * (imageView.image?.size.height)!
    imageView.frame = CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight)
}

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