如何添加可缩放图像

how to add zoomable image

目前我正在实现一个显示图像的片段,但我尝试使用 https://github.com/chrisbanes/PhotoView 但它崩溃了

代码如下:

public class StorelayoutFragment extends Fragment {
ImageView mImageView;
PhotoViewAttacher mAttacher;

public StorelayoutFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
  View view=inflater.inflate(R.layout.fragment_store_layout, container, false);


    // Any implementation of ImageView can be used!
    mImageView = (ImageView) view.findViewById(R.id.imageView);

    // Set the Drawable displayed
    Drawable bitmap = getResources().getDrawable(R.drawable.small_layout);
    mImageView.setImageDrawable(bitmap);

    // Attach a PhotoViewAttacher, which takes care of all of the zooming functionality.
    // (not needed unless you are going to change the drawable later)
    mAttacher = new PhotoViewAttacher(mImageView);
return view;
}}

我得到的错误是:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.sp.ez_mart_z, PID: 18272
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference
                  at com.sp.ez_mart_z.StorelayoutFragment.onCreateView(StorelayoutFragment.java:42)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
                  at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:541)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6247)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

有什么建议吗?

问题似乎是 mImageView 为空。您应该检查为什么在该视图中找不到 R.id.imageView

我解决了我的问题

她的代码对不起我的愚蠢...

public class StorelayoutFragment extends Fragment {

ImageView imageview;
public StorelayoutFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view=inflater.inflate(R.layout.fragment_store_layout, container, false);

    imageview = (ImageView)view.findViewById(R.id.imageView4);
    PhotoViewAttacher photoView = new PhotoViewAttacher(imageview);
            photoView.update();
return view;
}}

她的 xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sp.ez_mart_z.StoreLayoutFragment">

<!-- TODO: Update blank fragment layout -->

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/small_layout"
    android:id="@+id/imageView4" />