Reputation: 896
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>
Upvotes: 0
Views: 1043
Reputation: 11141
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
Upvotes: 1
Reputation: 11141
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,
};
}
Upvotes: 1
Reputation: 26547
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.
Upvotes: 7