title: Android photo gallery
body: <p>I have created an application with fragments and on gallery fragment whenever I open that fragment it gets closed.</p>
  
  <p>following is the code:</p>
  
  <p>.xml:</p>
  
  <pre><code>&lt;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"&gt;
  
      &lt;Gallery
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/gallery1"
          /&gt;
  
      &lt;ImageView
          android:layout_width="wrap_content"
          android:layout_marginTop="25dp"
          android:layout_height="wrap_content"
          android:id="@+id/imageView1"
          /&gt;
  
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>and coding:</p>
  
  <pre><code>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&lt;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&lt;?&gt; 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;
          }
      }
  
  }
  </code></pre>
  
  <p>Logs:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>I want to create a photo gallery with one gallery and Imageview. in Gallery it will display all the images from drawable folder with name img0,img1,img2, etc... and the selected image will be display on image view.</p>
  
  <p>Someone help me please solving this problem.</p>
  
  <p>Thanks :)</p>