Android照片库

时间:2017-02-18 17:38:25

标签: android android-imageview

每当我打开它关闭的片段时,我就创建了一个带有片段和gallery片段的应用程序。

以下是代码:

.XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="junagadh.pkmcollege.com.pkmcollege.bscFragment">

    <Gallery
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gallery1"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_marginTop="25dp"
        android:layout_height="wrap_content"
        android:id="@+id/imageView1"
        />



</LinearLayout>

和编码:

public class bscFragment extends Fragment {

    int img[] = new int[5];
    public bscFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        for(int i=0;i<img.length;i++) {
            img[i]=getResources().getIdentifier("img"+i,"drawable",getActivity().getPackageName());
        }

        final ImageView iv=(ImageView)getView().findViewById(R.id.imageView1);
        iv.setImageResource(img[0]);

        Gallery gal=(Gallery)getView().findViewById(R.id.gallery1);
        gal.setAdapter(new ImageAdapter(getActivity()));
        gal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                iv.setImageResource(img[pos]);
            }
        });

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_bsc, container, false);
    }

    public class ImageAdapter extends BaseAdapter
    {
        Context c;
        public ImageAdapter(Context c){
            this.c=c;
        }


        @Override
        public int getCount() {
            return img.length;
        }

        @Override
        public Object getItem(int arg0) {
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public View getView(int pos, View arg1, ViewGroup arg2) {
            ImageView iv=new ImageView(c);
            iv.setImageResource(img[pos]);
            iv.setLayoutParams(new Gallery.LayoutParams(120,150));
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            return iv;
        }
    }

}

日志:

I/OpenGLRenderer: Initialized EGL, version 1.4
E/EGL_emulation: tid 3280: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa237f040, error=EGL_BAD_MATCH
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: junagadh.pkmcollege.com.pkmcollege, PID: 3263
                  java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                      at junagadh.pkmcollege.com.pkmcollege.bscFragment.onCreateView(bscFragment.java:34)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                      at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
                      at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
                      at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/Process: Sending signal. PID: 3263 SIG: 9
Application terminated.

我想创建一个包含一个图库和Imageview的照片库。在Gallery中,它将显示来自可绘制文件夹的所有图像,名称为img0,img1,img2等......所选图像将显示在图像视图中。

有人帮我解决这个问题。

谢谢:)

1 个答案:

答案 0 :(得分:1)

您可以先对视图进行充气,然后从充气视图中获取项目。然后返回你膨胀的视图。给定错误日志上的空指针异常,以下内容应该有效。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bsc, container, false);
    for(int i=0;i<img.length;i++) {
        img[i]=getResources().getIdentifier("img"+i,"drawable",getActivity().getPackageName());
    }

    final ImageView iv=(ImageView)view.findViewById(R.id.imageView1);
    iv.setImageResource(img[0]);

    Gallery gal=(Gallery)view.findViewById(R.id.gallery1);
    gal.setAdapter(new ImageAdapter(getActivity()));
    gal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
            iv.setImageResource(img[pos]);
        }
    });

    return view;
}