我在先获取ImageView对象,后执行ImageSwitcher.setFactory(new ViewSwitcher.ViewFactory(){});就会出现上面异常。反之就正常,请问产生错误原理是什么。 注释地方为错误处
private ImageSwitcher switcher; private ImageView imageView; private int foot = 0; private int[] imgRes = new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inintView(); switcher.setFactory(new ViewSwitcher.ViewFactory() { // !!!!!!!!!!!!!!!!!!! @Override public View makeView() { imageView.setBackgroundColor(0xFFFFFFFF); imageView.setScaleType(ImageView.ScaleType.CENTER); imageView.setLayoutParams(new ImageSwitcher.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); return imageView; } }); switcher.setImageResource(imgRes[++foot]); switcher.setInAnimation(this, android.R.anim.fade_in); switcher.setOutAnimation(this, android.R.anim.fade_out); } void inintView() { switcher = (ImageSwitcher) findViewById(R.id.imgView); findViewById(R.id.btnPrevious).setOnClickListener(new OnClickListenerImp()); findViewById(R.id.btnNext).setOnClickListener(new OnClickListenerImp()); imageView = new ImageView(MainActivity.this); // !!!!!!!!!!!!!!!!!!! }