Gallery为什么闪退?
MainActivity部分:
package com.example1.administrator.gallerydemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher; public class MainActivity extends AppCompatActivity implements OnItemSelectedListener, ViewSwitcher.ViewFactory{ private int[] img={R.drawable.img_1,R.drawable.img_2,R.drawable.img_3,R.drawable.img_4,R.drawable.img_5,R.drawable.img_6}; private Gallery gallery; private ImageSwitcher is; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gallery= (Gallery) findViewById(R.id.gallery); is = (ImageSwitcher) findViewById(R.id.imageSwitcher); ImageAdapter adapter =new ImageAdapter(this,img); gallery.setAdapter(adapter); gallery.setOnItemSelectedListener(this); is.setFactory(this); is.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in)); is.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out)); } @Override public View makeView() { ImageView image =new ImageView(this); image.setScaleType(ImageView.ScaleType.FIT_CENTER); return image; } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { is.setBackgroundResource(img[position%img.length]); } @Override public void onNothingSelected(AdapterView<?> parent) { } } 适配器部分: package com.example1.administrator.gallerydemo; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; /** * Created by Administrator on 2016/10/5. */ public class ImageAdapter extends BaseAdapter { private int[] img; private Context context; public ImageAdapter(Context context, int []img) { this.img = img; this.context=context; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public Object getItem(int position) { return img[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView image = new ImageView(context); image.setBackgroundResource(img[position%img.length]); image.setLayoutParams(new Gallery.LayoutParams(200,150)); image.setScaleType(ImageView.ScaleType.FIT_XY); return image; } } xml布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example1.administrator.gallerydemo.MainActivity"> <Gallery android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageSwitcher android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/imageSwitcher" /> </LinearLayout>