javaandroidfragmentandroid-photoview

how to add zoomable image


Currently i am implementing a fragment to display an image but i tried using https://github.com/chrisbanes/PhotoView and it crash

Here is the code:

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

Error that i get is:

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$1.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)

Any suggestions?


Solution

  • The problem seems to be that mImageView is null. You should check why R.id.imageView cannot be found in that view.