Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:13:21+00:00 2026-06-06T15:13:21+00:00

What i have done so far is public class GalleryView extends Activity { public

  • 0

What i have done so far is

public class GalleryView extends Activity {
public Context context=getApplicationContext();

public Map< Integer,Drawable> dayMap;
Bitmap icon1 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica1);
Drawable d1 =new BitmapDrawable(context.getResources(),icon1);

Bitmap icon2 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica2);
Drawable d2 =new BitmapDrawable(context.getResources(),icon2);

Bitmap icon3 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica3);
Drawable d3 =new BitmapDrawable(context.getResources(),icon3);

Bitmap icon4 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica4);
Drawable d4 =new BitmapDrawable(context.getResources(),icon4);

Bitmap icon5 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica5);
Drawable d5 =new BitmapDrawable(context.getResources(),icon5);

ImageView imageView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_gallery);
    dayMap=new HashMap<Integer,Drawable>();
    dayMap.put(0,d1);
    dayMap.put(1,d2);
    dayMap.put(2,d3);
    dayMap.put(3,d4);
    dayMap.put(4,d5);

    Gallery ga = (Gallery)findViewById(R.id.Gallery01);
    ga.setAdapter(new ImageAdapter(context));

    imageView = (ImageView)findViewById(R.id.ImageView01);
    ga.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(getBaseContext(), 
                    "You have selected picture " + (arg2+1) + " of Antartica", 
                    Toast.LENGTH_SHORT).show();
            imageView.setImageDrawable(dayMap.get(arg2));

        }

    });

}


public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;

    public ImageAdapter(Context c) {
        ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
        imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
        ta.recycle();
    }

    @Override
    public int getCount() {

        return dayMap.size();
    }

    @Override
    public Object getItem(int arg0) {

        return arg0;
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView iv = new ImageView(ctx);
        iv.setImageDrawable(dayMap.get(arg0));
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(160,130));
        iv.setBackgroundResource(imageBackground);
        return iv;
    }

}

}

But i am getting this error-

06-26 17:29:27.171: E/AndroidRuntime(853): FATAL EXCEPTION: main
06-26 17:29:27.171: E/AndroidRuntime(853): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sai.samples.views/com.sai.samples.views.GalleryView}: java.lang.NullPointerException
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.os.Looper.loop(Looper.java:130)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-26 17:29:27.171: E/AndroidRuntime(853):  at java.lang.reflect.Method.invokeNative(Native Method)
06-26 17:29:27.171: E/AndroidRuntime(853):  at java.lang.reflect.Method.invoke(Method.java:507)
06-26 17:29:27.171: E/AndroidRuntime(853):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-26 17:29:27.171: E/AndroidRuntime(853):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-26 17:29:27.171: E/AndroidRuntime(853):  at dalvik.system.NativeStart.main(Native Method)
06-26 17:29:27.171: E/AndroidRuntime(853): Caused by: java.lang.NullPointerException
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
06-26 17:29:27.171: E/AndroidRuntime(853):  at com.sai.samples.views.GalleryView.<init>(GalleryView.java:24)
06-26 17:29:27.171: E/AndroidRuntime(853):  at java.lang.Class.newInstanceImpl(Native Method)
06-26 17:29:27.171: E/AndroidRuntime(853):  at java.lang.Class.newInstance(Class.java:1409)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
06-26 17:29:27.171: E/AndroidRuntime(853):  ... 11 more

Any help would be highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:13:22+00:00Added an answer on June 6, 2026 at 3:13 pm

    This line cause NPE

    ga.setAdapter(new ImageAdapter(context));
    

    Because your context is null, as you initialized it outside of your activity’s onCreate()

    Actually,

    These are your dead codes, (You can not get application context out-side of activity method or without reference of it)

    public Context context=getApplicationContext();
    Bitmap icon1 = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.antartica1);
    Drawable d1 =new BitmapDrawable(context.getResources(),icon1);
    Bitmap icon2 = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.antartica2);
    Drawable d2 =new BitmapDrawable(context.getResources(),icon2);
    Bitmap icon3 = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.antartica3);
    Drawable d3 =new BitmapDrawable(context.getResources(),icon3);
    Bitmap icon4 = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.antartica4);
    Drawable d4 =new BitmapDrawable(context.getResources(),icon4);
    Bitmap icon5 = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.antartica5);
    Drawable d5 =new BitmapDrawable(context.getResources(),icon5);
    

    You can not do it in out-side of onCreate() or any other method of Activity.

    So put your these code in onCreate() method of activity..

    Like,

    public Context context;
    Bitmap icon1, icon2, icon3, icon4, icon5;
    Drawable d1, d2, d3, d4, d5; 
    public Map< Integer,Drawable> dayMap;
    ImageView imageView;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.main_gallery);
     context=getApplicationContext();
     icon1 = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.antartica1);
     d1 =new BitmapDrawable(context.getResources(),icon1);
     icon2 = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.antartica2);
     d2 =new BitmapDrawable(context.getResources(),icon2);
     icon3 = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.antartica3);
     d3 =new BitmapDrawable(context.getResources(),icon3);
     icon4 = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.antartica4);
     d4 =new BitmapDrawable(context.getResources(),icon4);
     icon5 = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.antartica5);
     d5 =new BitmapDrawable(context.getResources(),icon5);
     .
     .
     .
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is what I have so far: public class Numbers { // Fields public
So I have the following code: import java.lang.Thread; import java.lang.Integer; class MyThread extends Thread
I have code similar to the following: public class myButton extends JButton() { public
I have not done any animation stuff with iphone development so far. Can anybody
I have the following entity public class Employee { public int EmployeeID { get;
I have an entity like this: public class Employment { public virtual Company Company
I have a problem with JPA 1.0 (OpenJPA) Following situation @Entity public class A{
I have a class Article: public class Article { public int Id { get;
I've been trying to load a bitmap in a non-activity class but everything I've
Have done some research and found some stuff that may be helpful. I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.