public class MainActivity extends ActionBarActivity {
Gallery gallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery=(Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
long imageId = ImageAdapter.ThumbsIds[position];
Intent fullScreenIntent = new Intent(v.getContext(), FullScreenImage.class);
fullScreenIntent.putExtra(MainActivity.class.getName(), imageId);
MainActivity.this.startActivity(fullScreenIntent);
}
});
}
ImageAdapter:
public class ImageAdapter extends BaseAdapter implements SpinnerAdapter {
private Context context;
public ImageAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return ThumbsIds.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return ThumbsIds[position];
}
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView=null;
if(convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(215, 200));
imageView.setPadding(8, 8, 8, 8);
}else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(ThumbsIds[position]);
return imageView;
}
public static Integer[] ThumbsIds={
R.drawable.abs_icla,
R.drawable.abs_dog,
R.drawable.abs_flow,
R.drawable.abs_neb,
R.drawable.abs_rad
};
}
FullScreenImage
public class FullScreenImage extends Activity {
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.full_screen);
Intent intent = getIntent();
long imageId = (Long) intent.getExtras().get(FullScreenImage.class.getName());
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setLayoutParams( new LinearLayout.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
imageView.setImageResource((int) imageId);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
} }
logcat的:
09-25 10:02:47.188:E / AndroidRuntime(1345):致命异常:主要 09-25 10:02:47.188:E / AndroidRuntime(1345):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.test4 / com.example.test4.FullScreenImage}:java.lang.NullPointerException 09-25 10:02:47.188:E / AndroidRuntime(1345):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 09-25 10:02:47.188:E / AndroidRuntime(1345):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 09-25 10:02:47.188:E / AndroidRuntime(1345):在android.app.ActivityThread.access $ 600(ActivityThread.java:130) 09-25 10:02:47.188:E / AndroidRuntime(1345):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195) 09-25 10:02:47.188:E / AndroidRuntime(1345):在android.os.Handler.dispatchMessage(Handler.java:99) 09-25 10:02:47.188:E / AndroidRuntime(1345):在android.os.Looper.loop(Looper.java:137) 09-25 10:02:47.188:E / AndroidRuntime(1345):在android.app.ActivityThread.main(ActivityThread.java:4745) 09-25 10:02:47.188:E / AndroidRuntime(1345):at java.lang.reflect.Method.invokeNative(Native Method) 09-25 10:02:47.188:E / AndroidRuntime(1345):at java.lang.reflect.Method.invoke(Method.java:511) 09-25 10:02:47.188:E / AndroidRuntime(1345):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
我的full_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
尝试清除该应用无效。