attempt to invoke method crash(updated) encountered new error

57 Views Asked by At

hi currently i encountered this problem

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.sp.ez_mart_z, PID: 31934
              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:41)
                  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)

Application terminated.

i cant seem to figure out how to solve this heres my code. this code is suppose to display an imageview after pressing on on the options in the app drawer

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
  inflater.inflate(R.layout.fragment_store_layout, container, false);


    // Any implementation of ImageView can be used!
    mImageView = (ImageView) getView().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 null;
}

}

1

There are 1 best solutions below

2
On

Need to return inflate layout from onCreateView and use same View instance for accessing View's from it. do following changes in onCreateView:

View view= inflater.inflate(R.layout.fragment_store_layout, container, false);
 mImageView = (ImageView) view.findViewById(R.id.imageView);

.....

return view;