小弟我最近在学习编写拖动图标的程序,现在遇到的问题时:下面的代码可以在模拟器上面跑起来,但是当我向左拖动到第四幅图片是,就会出现如下错误: ERROR/AndroidRuntime(600): FATAL EXCEPTION: main, ERROR/AndroidRuntime(600): java.lang.NullPointerException,希望哪位高手能够指点一下小弟
代码如下:package com.sky;import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Matrix;
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.AdapterView.OnItemSelectedListener;public class RecialGalleryActivity extends Activity implements OnItemSelectedListener{
private Gallery gallery;
// private ImageSwitcher imageSwitcher;
private ImageAdapter imageAdapter; private int[] resIds = new int[]
{ R.drawable.img1, R.drawable.img2, R.drawable.img3,
R.drawable.img4,
R.drawable.img5, R.drawable.img6, R.drawable.img7,
R.drawable.img8 }; public class ImageAdapter extends BaseAdapter
{
int mGalleryItemBackground;
private Context mContext; public ImageAdapter(Context context)
{
mContext = context;
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
}
// 第1点改进,返回一个很大的值,例如,Integer.MAX_VALUE
public int getCount()
{
return resIds.length;
} public Object getItem(int position)
{
return position;
} public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView = new ImageView(mContext);
// 第2点改进,通过取余来循环取得resIds数组中的图像资源ID
imageView.setImageResource(resIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(200, 200));
// 设置显示比例类型
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position,long id)
{
// 选中Gallery中某个图像时,在ImageSwitcher组件中放大显示该图像
//我估计问题出现在这个函数中,但是自己试了一些改动仍然不成功,哪位高手能够指点一下
//imageSwitcher.setImageResource(resIds[position]);
/* view.setLayoutParams(new Gallery.LayoutParams(300,300));
int mCounts = gallery.getCount() - 1;
if((position>0) && (position < mCounts)){
gallery.getChildAt(position - 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
gallery.getChildAt(position + 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
}
if(position == 0){
gallery.getChildAt(position + 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
}
if(position == mCounts){
gallery.getChildAt(position - 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
}**/
gallery.getChildAt(position).setLayoutParams(new Gallery.LayoutParams(300,300));
int mCounts = 2*gallery.getCount() - 1;
if((position>0) && (position < mCounts)){
gallery.getChildAt(position - 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
gallery.getChildAt(position + 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
}
if(position == 0){
gallery.getChildAt(position + 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
}
if(position == mCounts){
gallery.getChildAt(position - 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
}
}
public void onNothingSelected(AdapterView<?> parent)
{
} /** @Override
// ImageSwitcher组件需要这个方法来创建一个View对象(一般为ImageView对象)
// 来显示图像
public View makeView()
{
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
**/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.gallery);
imageAdapter = new ImageAdapter(this);
gallery.setAdapter(imageAdapter);
gallery.setOnItemSelectedListener(this);
// imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
// 设置ImageSwitcher组件的工厂对象
// imageSwitcher.setFactory(this);
// 设置ImageSwitcher组件显示图像的动画效果
// imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
// android.R.anim.fade_in));
// imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
// android.R.anim.fade_out)); }
}
代码如下:package com.sky;import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Matrix;
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.AdapterView.OnItemSelectedListener;public class RecialGalleryActivity extends Activity implements OnItemSelectedListener{
private Gallery gallery;
// private ImageSwitcher imageSwitcher;
private ImageAdapter imageAdapter; private int[] resIds = new int[]
{ R.drawable.img1, R.drawable.img2, R.drawable.img3,
R.drawable.img4,
R.drawable.img5, R.drawable.img6, R.drawable.img7,
R.drawable.img8 }; public class ImageAdapter extends BaseAdapter
{
int mGalleryItemBackground;
private Context mContext; public ImageAdapter(Context context)
{
mContext = context;
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
}
// 第1点改进,返回一个很大的值,例如,Integer.MAX_VALUE
public int getCount()
{
return resIds.length;
} public Object getItem(int position)
{
return position;
} public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView = new ImageView(mContext);
// 第2点改进,通过取余来循环取得resIds数组中的图像资源ID
imageView.setImageResource(resIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(200, 200));
// 设置显示比例类型
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position,long id)
{
// 选中Gallery中某个图像时,在ImageSwitcher组件中放大显示该图像
//我估计问题出现在这个函数中,但是自己试了一些改动仍然不成功,哪位高手能够指点一下
//imageSwitcher.setImageResource(resIds[position]);
/* view.setLayoutParams(new Gallery.LayoutParams(300,300));
int mCounts = gallery.getCount() - 1;
if((position>0) && (position < mCounts)){
gallery.getChildAt(position - 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
gallery.getChildAt(position + 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
}
if(position == 0){
gallery.getChildAt(position + 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
}
if(position == mCounts){
gallery.getChildAt(position - 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
}**/
gallery.getChildAt(position).setLayoutParams(new Gallery.LayoutParams(300,300));
int mCounts = 2*gallery.getCount() - 1;
if((position>0) && (position < mCounts)){
gallery.getChildAt(position - 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
gallery.getChildAt(position + 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
}
if(position == 0){
gallery.getChildAt(position + 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
}
if(position == mCounts){
gallery.getChildAt(position - 1).setLayoutParams(new Gallery.LayoutParams(200, 200));
}
}
public void onNothingSelected(AdapterView<?> parent)
{
} /** @Override
// ImageSwitcher组件需要这个方法来创建一个View对象(一般为ImageView对象)
// 来显示图像
public View makeView()
{
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
**/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.gallery);
imageAdapter = new ImageAdapter(this);
gallery.setAdapter(imageAdapter);
gallery.setOnItemSelectedListener(this);
// imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
// 设置ImageSwitcher组件的工厂对象
// imageSwitcher.setFactory(this);
// 设置ImageSwitcher组件显示图像的动画效果
// imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
// android.R.anim.fade_in));
// imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
// android.R.anim.fade_out)); }
}
解决方案 »
- 最新的ADT开发html5 的问题
- htcG10下载图片无法显示
- service启动dialog问题
- android小问题,急求!
- 自定义View里面怎样较好地绘制文本
- 如何动态改变 AbsoluteLayout布局中其它布局的坐标,急
- 类似与QQ、微信的通信功能大概是怎么实现的?
- SQLite数据库查询错误
- 关于android的res中的文件删除问题, 删除了还是存在是怎么回事,
- 为什么FindViewById不用new一个对象就能直接赋值给某个引用变量?
- 求助,Android ListView notifyDatasetChanged刷新太快,UI无响应。。
- EditText如何屏蔽软键盘?
有空对象
java.lang.NullPointerException