我有4个包含不同图像的按钮,
我想在gridview中打开图像,当我点击模拟器中的图像时,它应该以新的布局显示图像(它在第一个adaper类中正常工作),但是当我在第二个按钮上打开图像时,模拟器中的图像它显示了适配器类2中的图像,但是当我单击图像时,它显示第一个适配器类中的图像
ImageActivity.java
public class ImageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_selection);
Button a,b,c,d;
b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(ImageActivity.this, screen2.class);
startActivity(i);
}
});
}
}
screen2.java
public class screen2 extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter2(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i = new Intent(getApplicationContext(), FullImageActivity1.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
ImageAdapter.java
public class ImageAdapter2 extends BaseAdapter {
private Context mContext;
public Integer[] images = {
R.drawable.baby1, R.drawable.baby2,
R.drawable.baby3,R.drawable.baby4,
};
public ImageAdapter2(Context c) {
mContext = c;
}
public int getCount() {
return images.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(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(60, 60));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(images[position]);
return imageView;
}
}
FullImageActivity.java
public class FullImageActivity2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image2);
Intent intent = getIntent();
int position = intent.getExtras().getInt("id");
ImageAdapter2 imageAdapter = new ImageAdapter2(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.images[position]);
}
}
logcat是
07-19 15:24:24.627: E/AndroidRuntime(1709): FATAL EXCEPTION: main
07-19 15:24:24.627: E/AndroidRuntime(1709): java.lang.RuntimeException: Unable to start activity ComponentInfo{abhinai.image/abhinai.image.FullImageActivity3}: java.lang.NullPointerException
07-19 15:24:24.627: E/AndroidRuntime(1709): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-19 15:24:24.627: E/AndroidRuntime(1709): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-19 15:24:24.627: E/AndroidRuntime(1709): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-19 15:24:24.627: E/AndroidRuntime(1709): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-19 15:24:24.627: E/AndroidRuntime(1709): at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 15:24:24.627: E/AndroidRuntime(1709): at android.os.Looper.loop(Looper.java:137)
07-19 15:24:24.627: E/AndroidRuntime(1709): at android.app.ActivityThread.main(ActivityThread.java:5039)
07-19 15:24:24.627: E/AndroidRuntime(1709): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 15:24:24.627: E/AndroidRuntime(1709): at java.lang.reflect.Method.invoke(Method.java:511)
07-19 15:24:24.627: E/AndroidRuntime(1709): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-19 15:24:24.627: E/AndroidRuntime(1709): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-19 15:24:24.627: E/AndroidRuntime(1709): at dalvik.system.NativeStart.main(Native Method)
07-19 15:24:24.627: E/AndroidRuntime(1709): Caused by: java.lang.NullPointerException
07-19 15:24:24.627: E/AndroidRuntime(1709): at abhinai.image.FullImageActivity3.onCreate(FullImageActivity3.java:19)
07-19 15:24:24.627: E/AndroidRuntime(1709): at android.app.Activity.performCreate(Activity.java:5104)
07-19 15:24:24.627: E/AndroidRuntime(1709): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-19 15:24:24.627: E/AndroidRuntime(1709): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-19 15:24:24.627: E/AndroidRuntime(1709): ... 11 more
任何人都可以帮助我