title: create gallery image using gridView and intent
body: <p>I am creating a gallery image android application where  the user can choose any picture from the set of pictures that are existing in the <code>GridView</code> and the system will get back the selected item to the first activity and display the image.</p>
  
  <p>The problem is when i click on the button to go to the second activity (ImageGalleryActivity)the gridview do not exist and the set of pictures are not existing can anyone help me to fix this problem ???</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.devleb.harrypottergallery;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.ImageView;
  
  public class MainActivity extends Activity implements OnClickListener {
  
      ImageView imgBig, imgBtn;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      imgBig = (ImageView)findViewById(R.id.imgv);
      imgBtn = (ImageView)findViewById(R.id.btn_img);
      imgBtn.setOnClickListener(this);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
  
          Intent i = new Intent(this, ImageGalleryActivity.class);
          startActivity(i);
      }
  
  }
  </code></pre>
  
  <p>ImageGalleryActivity.java</p>
  
  <pre><code>package com.devleb.harrypottergallery;
  
  import android.app.Activity;
  import android.content.Context;
  import android.os.Bundle;
  import android.test.suitebuilder.annotation.SmallTest;
  import android.view.Menu;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.BaseAdapter;
  import android.widget.GridView;
  import android.widget.ImageView;
  
  public class ImageGalleryActivity extends Activity implements
          OnItemClickListener {
  
      GridView gridV;
  
      ImageView imgg;
  
      // array of small images
      public Integer[] smallImage = new Integer[] { R.drawable.smallpic01,
              R.drawable.smallpic02, R.drawable.smallpic03,
              R.drawable.smallpic04, R.drawable.smallpic05,
              R.drawable.smallpic06, R.drawable.smallpic07,
              R.drawable.smallpic08, R.drawable.smallpic09,
              R.drawable.smallpic10, R.drawable.smallpic11,
              R.drawable.smallpic12, R.drawable.smallpic13,
              R.drawable.smallpic14, R.drawable.smallpic15,
              R.drawable.smallpic16, R.drawable.smallpic17,
              R.drawable.smallpic18, R.drawable.smallpic19,
              R.drawable.smallpic20, R.drawable.smallpic21,
              R.drawable.smallpic22, R.drawable.smallpic23,
              R.drawable.smallpic24, R.drawable.smallpic25,
              R.drawable.smallpic26, R.drawable.smallpic27,
              R.drawable.smallpic28, R.drawable.smallpic29, R.drawable.smallpic30 };
  
      // array of large images
      Integer[] largImage = new Integer[] { R.drawable.pic01, R.drawable.pic02,
              R.drawable.pic03, R.drawable.pic04, R.drawable.pic05,
              R.drawable.pic06, R.drawable.pic07, R.drawable.pic08,
              R.drawable.pic09, R.drawable.pic10, R.drawable.pic11,
              R.drawable.pic12, R.drawable.pic13, R.drawable.pic14,
              R.drawable.pic15, R.drawable.pic16, R.drawable.pic17,
              R.drawable.pic18, R.drawable.pic19, R.drawable.pic20,
              R.drawable.pic21, R.drawable.pic22, R.drawable.pic23,
              R.drawable.pic24, R.drawable.pic25, R.drawable.pic26,
              R.drawable.pic27, R.drawable.pic28, R.drawable.pic29,
              R.drawable.pic30 };
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_image_gallery);
  
          gridV = (GridView) findViewById(R.id.grid_v);
          gridV.setAdapter(new GalleryIMGAdapter(this));
          gridV.setOnItemClickListener(this);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.image_gallery, menu);
          return true;
      }
  
      @Override
      public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) {
          // TODO Auto-generated method stub
  
          ViewImage(arg2);
  
      }
  
      private void ViewImage(int arg2) {
          // TODO Auto-generated method stub
  
          setContentView(R.layout.activity_main);
          imgg = (ImageView) findViewById(R.id.imgv);
          imgg.setImageResource(largImage[arg2]);
      }
  
  }
  
  class GalleryIMGAdapter extends BaseAdapter {
  
      private Context mc;
  
      public GalleryIMGAdapter(Context c) {
          this.mc = c;
      }
  
      @Override
      public int getCount() {
          // TODO Auto-generated method stub
          return 0;
      }
  
      @Override
      public Object getItem(int arg0) {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public long getItemId(int arg0) {
          // TODO Auto-generated method stub
          return 0;
      }
  
      @Override
      public View getView(int position, View convertview, ViewGroup parent) {
          // TODO Auto-generated method stub
  
          ImageView img;
  
          if (convertview == null) {
              img = new ImageView(mc);
              img.setLayoutParams(new GridView.LayoutParams(200, 180));
              img.setScaleType(ImageView.ScaleType.CENTER_CROP);
              img.setPadding(8, 8, 8, 8);
          }
          return null;
      }
  
  }
  </code></pre>
  
  <p>ImageGalleryActivity.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".ImageGalleryActivity" &gt;
  
      &lt;GridView
          android:id="@+id/grid_v"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:layout_marginTop="18dp"
          android:columnWidth="90dp"
          android:gravity="center"
          android:horizontalSpacing="10dp"
          android:numColumns="3"
          android:stretchMode="columnWidth"
          android:verticalSpacing="10dp" &gt;
      &lt;/GridView&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>log cat </p>
  
  <pre><code>12-12 13:16:48.745: E/AndroidRuntime(1959): FATAL EXCEPTION: main
  12-12 13:16:48.745: E/AndroidRuntime(1959): java.lang.NullPointerException
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.widget.AbsListView.obtainView(AbsListView.java:2163)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.widget.GridView.onMeasure(GridView.java:1040)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.View.measure(View.java:15848)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.View.measure(View.java:15848)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.View.measure(View.java:15848)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.View.measure(View.java:15848)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.View.measure(View.java:15848)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.Choreographer.doFrame(Choreographer.java:532)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.os.Handler.handleCallback(Handler.java:730)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.os.Handler.dispatchMessage(Handler.java:92)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.os.Looper.loop(Looper.java:137)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.app.ActivityThread.main(ActivityThread.java:5103)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at java.lang.reflect.Method.invokeNative(Native Method)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at java.lang.reflect.Method.invoke(Method.java:525)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  12-12 13:16:48.745: E/AndroidRuntime(1959):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>