package com.gqs.GalleryTest;import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ViewSwitcher.ViewFactory;
public class ChangeLogo extends Activity implements ViewFactory{
private int currentImage;
private RelativeLayout layout;
private Gallery g;
private ImageSwitcher is;
private ImageButton imbtnLogo;
private int nowImage;
private int[] images = new int[] { R.drawable.icon, R.drawable.image1,
R.drawable.image2, R.drawable.image3, R.drawable.image4,
R.drawable.image5, R.drawable.image6, R.drawable.image7,
R.drawable.image8, R.drawable.image9, R.drawable.image10,
R.drawable.image11, R.drawable.image12, R.drawable.image13,
R.drawable.image14, R.drawable.image15, R.drawable.image16,
R.drawable.image17, R.drawable.image18, R.drawable.image19,
R.drawable.image20, R.drawable.image21, R.drawable.image22,
R.drawable.image23, R.drawable.image24, R.drawable.image25,
R.drawable.image26, R.drawable.image27, R.drawable.image28,
R.drawable.image29, R.drawable.image30 };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.changelogo);
LayoutInflater inflater=LayoutInflater.from(getApplicationContext());
layout=(RelativeLayout)inflater.inflate(R.layout.dialoglayout, null);
g=(Gallery)layout.findViewById(R.id.galleryChange);
is=(ImageSwitcher)layout.findViewById(R.id.logoImageSwitcher);
is.setFactory(this);
is.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in));
is.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_out));
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
   
   currentImage=images[position];
   is.setImageResource(currentImage);
} @Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}



});
imbtnLogo=(ImageButton)findViewById(R.id.imbtnLogo);
imbtnLogo.setOnClickListener(new OnClickListener()
{ @Override
public void onClick(View v) {
InitDialog();

}

});


}
public void InitDialog()
{


 new AlertDialog.Builder(ChangeLogo.this).setTitle("选择头像")
                                                 .setMessage("请选择头像")
                                                 .setView(layout)
                                                 .setPositiveButton("确定", 
                                                  new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
nowImage=currentImage;
imbtnLogo.setImageResource(currentImage);

}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

imbtnLogo.setImageResource(nowImage);
}
}).show();
}
private class ImageAdapter extends BaseAdapter
{ private Context context; public ImageAdapter(Context c) {
this.context = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {

ImageView iv=new ImageView(context);
iv.setImageResource(images[position%images.length]);
iv.setLayoutParams(new LayoutParams(90,120));
iv.setPadding(5,5,5,5);
iv.setAdjustViewBounds(true);
return iv;
}


}
@Override
public View makeView() {
ImageView iv=new ImageView(this);
iv.setAdjustViewBounds(true);
iv.setPadding(10, 10, 10, 10);
return iv;
}
}
我想在一个AlertDialog中实现一个换头像的效果,每次总报
08-17 17:30:53.923: ERROR/AndroidRuntime(1061): FATAL EXCEPTION: main
08-17 17:30:53.923: ERROR/AndroidRuntime(1061): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.widget.Gallery.setUpChild(Gallery.java:769)
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.widget.Gallery.makeAndAddView(Gallery.java:738)
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.widget.Gallery.layout(Gallery.java:622)
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.widget.Gallery.onLayout(Gallery.java:336)
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.view.View.layout(View.java:7035)
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.view.View.layout(View.java:7035)
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.view.View.layout(View.java:7035)
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.view.View.layout(View.java:7035)
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
08-17 17:30:53.923: ERROR/AndroidRuntime(1061):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
这些错,哪儿错了?

解决方案 »

  1.   

    ClassCastException   强制类型转换错误异常
      

  2.   

    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    ImageButton
    第一次点击正常 ,第二次再点 就报上面的错误 什么意思
      

  3.   

    ClassCastException强制转换异常,你在哪里转换出错了。找找看
      

  4.   

    你的布局里写的 跟你程序里用的不一样吧。
    比如你布局里写的view 你程序里用的imageview这样。