title: ClassCastException: android.app.Application
body: <p>The class which causes error is given below:</p>
  
  <pre><code>package com.extrasmart;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  //import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.BaseAdapter;
  import android.widget.GridView;
  import android.widget.ImageView;
  import android.widget.AdapterView.OnItemSelectedListener;
  
  public class ActivityImgGrid extends Activity {
  
      ExtraSmartApplication application = (ExtraSmartApplication) getApplication();
  
      GridView mGrid;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activityimg_grid);
          mGrid = (GridView) findViewById(R.id.imgGrid);
          mGrid.setAdapter(new AppsAdapter());
          mGrid.setOnItemSelectedListener(new OnItemSelectedListener()
          {
              public void onNothingSelected(AdapterView v) {
                  // TODO Auto-generated method stub
  
              }
  
              public void onItemSelected(AdapterView parent, View v, int position, long id) {
                  ExtraSmartApplication application = (ExtraSmartApplication) getApplication();
  
                  application.setSelCategoryIcon(position);
                  setResult(RESULT_OK);        
                  finish();
  
              }
  
          });
  
      }
  
      public class AppsAdapter extends BaseAdapter {
          ExtraSmartApplication application = (ExtraSmartApplication) getApplication();
          private Integer[] imageIDs = application.getCategoryIcons();
  
          public AppsAdapter() {
          }
  
          public View getView(int position, View convertView, ViewGroup parent) {
              ImageView i;
  
              if (convertView == null) {
                  i = new ImageView(ActivityImgGrid.this);
                  //i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                  i.setLayoutParams(new GridView.LayoutParams(48, 48));
                  i.setScaleType(ImageView.ScaleType.CENTER_CROP);
                  i.setPadding(5, 5, 5, 5);
              } else {
                  i = (ImageView) convertView;
              }
  
              i.setImageResource(imageIDs[position]);
              return i;
         }
  
  
          public final int getCount() {
              return imageIDs.length;
          }
  
          public final Object getItem(int position) {
              return imageIDs[position];
          }
  
          public final long getItemId(int position) {
              return position;
          }
      }
  
  }
  </code></pre>
  
  <p>The xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/imgGrid"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"
      android:verticalSpacing="10dp"
      android:horizontalSpacing="10dp"
      android:numColumns="auto_fit"
      android:columnWidth="48px"
      android:stretchMode="columnWidth"    
      android:gravity="center"
      android:minHeight="48px"
      android:minWidth="48px"
      /&gt;
  </code></pre>
  
  <p>Class used by ActivityImgGrid class:</p>
  
  <pre><code>public class ExtraSmartApplication extends Application {
  
      //private static final String APP_CREDENTIALS = null;
      private Integer selCategoryImg;
      private boolean isCardImageNew = false;
      private byte[] cardImage = null;
  
      private Integer[] categoryImgs = {
              R.drawable.cricket,
              R.drawable.football,
              R.drawable.vollyball
      };
  
      private boolean isCatCreated = false;
  
      public ExtraSmartApplication() {
          super();
      }
  
      @Override
      public void onTerminate() {
          super.onTerminate();
      }
  
  
      public Integer getSelCategoryIcon() {
          return this.selCategoryImg;
      }
  
      public Integer[] getCategoryIcons() {
          return this.categoryImgs;
      }
  
      public void setSelCategoryIcon(int position) {
          this.selCategoryImg = categoryImgs[position];
      }
  
      public void setCatCreated(boolean isCatCreated) {
          this.isCatCreated = isCatCreated;
      }
  
      public boolean isCatCreated() {
          return isCatCreated;
      }
  
      public void setCardImageNew(boolean isCardImageNew) {
          this.isCardImageNew = isCardImageNew;
      }
  
      public boolean isCardImageNew() {
          return isCardImageNew;
      }
  
      public void setCardImage(byte[] cardImage) {
          this.cardImage = cardImage;
      }
  
      public byte[] getCardImage() {
          return cardImage;
      }
  }
  </code></pre>