0

I am trying to make photo gallery application.But I am having following error and my program will crashed.Also I am having error to this line :

imageView.setImageResource(pics[arg2]);

error

09-03 16:45:22.618: E/AndroidRuntime(443): Uncaught handler: thread main exiting due to uncaught exception
09-03 16:45:22.628: E/AndroidRuntime(443): java.lang.NullPointerException
09-03 16:45:22.628: E/AndroidRuntime(443):  at com.example.myfirstapplication.setwall$1.onItemClick(setwall.java:40)
09-03 16:45:22.648: E/dalvikvm(443): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

Here is my code:

package com.sai.samples.views;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GalleryView extends Activity {
    Integer[] pics = {
            R.drawable.antartica1,
            R.drawable.antartica2,
            R.drawable.antartica3,
            R.drawable.antartica4,
            R.drawable.antartica5,
            R.drawable.antartica6,
            R.drawable.antartica7,
            R.drawable.antartica8,
            R.drawable.antartica9,
            R.drawable.antartica10
    };
    ImageView imageView;

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

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

        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.setImageResource(pics[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 pics.length;
        }

        @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.setImageResource(pics[arg0]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setLayoutParams(new Gallery.LayoutParams(150,120));
            iv.setBackgroundResource(imageBackground);
            return iv;
        }

    }
}

here is my main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<Gallery 
    android:id="@+id/Gallery01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"></Gallery>
<ImageView 
    android:id="@+id/ImageView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></ImageView>

</LinearLayout>
5
  • please post the relevant part (setwall$1.onItemClick) Commented Sep 3, 2012 at 11:21
  • did you tried to debug your code? ... imageView is null, i think ... Commented Sep 3, 2012 at 11:22
  • what is the line which logcat pointing onItemClick(setwall.java:40) Commented Sep 3, 2012 at 11:23
  • @kaluwila at imageView.setImageResource(pics[arg2]); this line I am having error. Commented Sep 4, 2012 at 6:22
  • your code should work please clean and run again. Commented Sep 4, 2012 at 6:41

3 Answers 3

7

This line probably returns null :

imageView = (ImageView)findViewById(R.id.ImageView01);

Check if the ImageView id is correct, and try to clean and rebuild your project if the error persists.

Sign up to request clarification or add additional context in comments.

8 Comments

Are you sure? The listener is added to the gallery, not the ImageView.
@Dalmas I dont understand what exactly you are saying.Please explain
I mean it's very likely that there isn't an ImageView with the id ImageView01 in your main.xml file.
Can you post your main.xml file?
@Dalmas I have posted the main.xml file
|
1

I have used this code for creating a gallery of images,

public class GalleryViewActivity extends Activity 
{

    GridView gridView;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gridView = (GridView)findViewById(R.id.gridView);

           gridView.setAdapter(new ImageAdapter(this));
    }
}


class ImageAdapter extends BaseAdapter 
{
    ArrayList<String> images;

    private Context mContext;

    public ImageAdapter(Context c) 
    {
        mContext = c;
    }

    public int getCount() 
    {
        return mThumbIds.length;
    }

    public Object getItem(int position) 
    {
        return null;
    }

    public long getItemId(int position) 
    {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null)
        {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        }
        else 
        {
            imageView = (ImageView) convertView;
        }
           imageView.setClickable(true);

           imageView.setOnClickListener(new OnClickListener()
           {            
            @Override
            public void onClick(View v) 
            {
                Intent i = new Intent(v.getContext(), showImageClass.class);
                Toast.makeText(v.getContext(), mThumbIds.toString() + " Clicked", 5000).show();
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra("imageName", mThumbIds[position]);
                v.getContext().startActivity(i);
            }
        });
//         imageView.setImageDrawable(images);
//         imageView.setImageBitmap(images);
//         imageView.setImageResource(images[]);
        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

//     references to images
    private Integer[] mThumbIds = 
    {
            R.drawable.abc,
            R.drawable.def,
            R.drawable.klm, 
            R.drawable.pqr,
            R.drawable.rst, 
            R.drawable.wxy,
            R.drawable.xyz, 
    };
}

4 Comments

I want to show all images in only that activity.In this code you have added showImageClass.So I dont want to do that.can you please change this code?
Have you used this code. This code is to show gallery type view of Images, where all images are shown that are in drawable folder. showImage class is created to show large view of the image. whenever you click on any of the image from gallery view, 'showImage' class is called to show large view of the image clicked.
I am able to see gallery images only when I am clicking on it for showing large view it is stop working.
I am posting my code for showing both the gallery view and the large image.
1

First create a class GalleryViewActivity

public class GalleryViewActivity extends Activity 
{

    GridView gridView;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gridView = (GridView)findViewById(R.id.gridView);
        gridView.setAdapter(new ImageAdapter(this));
    }
}


class ImageAdapter extends BaseAdapter 
{
    ArrayList<String> images;

    private Context mContext;

    public ImageAdapter(Context c) 
    {
        mContext = c;
    }

    public int getCount() 
    {
        return mThumbIds.length;
    }

    public Object getItem(int position) 
    {
        return null;
    }

    public long getItemId(int position) 
    {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null)
        {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        }
        else 
        {
            imageView = (ImageView) convertView;
        }
           imageView.setClickable(true);

           imageView.setOnClickListener(new OnClickListener()
           {            
            @Override
            public void onClick(View v) 
            {
                Intent i = new Intent(v.getContext(), showImageClass.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra("position", "" + position);
                v.getContext().startActivity(i);
            }
        });
        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

//     references to images
    private Integer[] mThumbIds = 
    {
            R.drawable.abc,
            R.drawable.def,
            R.drawable.klm, 
            R.drawable.pqr,
            R.drawable.rst, 
            R.drawable.wxy,
            R.drawable.xyz, 
    };
}

Dont forget to add images in the drawable folder. the image should be same as you mentioned in the mThumbIds array, like i have used abc.jpg, def.jpg and so on.. Then you need to create a showImage.java class, here you take a imageview to show the large view of image clicked..

public class showImageClass extends Activity
{
    private Integer[] mThumbIds = 
    {
            R.drawable.abc,
            R.drawable.def,
            R.drawable.klm, 
            R.drawable.pqr,
            R.drawable.rst, 
            R.drawable.wxy,
            R.drawable.xyz, 
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showimage);
        int position = Integer.parseInt(this.getIntent().getStringExtra("position"));
        ImageView jpgView = (ImageView)findViewById(R.id.imageView);
        jpgView.setImageResource(mThumbIds[position]);
    }
}

This code has worked for me, it will definitely work.. Dont forget to add the relavent images and import the class libraries that are needed. Mention both the activities in android manifest file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.