0

目前我正在实现一个片段来显示图像,但我尝试使用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$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)

有什么建议么?

4

2 回答 2

0

我解决了我的 QN

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

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" />

于 2017-01-08T09:21:22.660 回答
0

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

于 2017-01-08T09:04:10.750 回答