运行的时候插入第二个表情的时候老报错,错误: ERROR/AndroidRuntime(7458): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 告诉帮忙解决一下public class MainActivity extends Activity {
private static String TAG="MainActivity";
private Button expressionButton;
private LayoutInflater inflater;
private GridView mGridView;
private EditText et;
private Dialog dialog;
private int[]face=new int[]
{ R.drawable.smiley_b_close,R.drawable.smiley_colon_apostrophe_open,
R.drawable.smiley_colon_close,R.drawable.smiley_colon_d,
R.drawable.smiley_colon_dollar,R.drawable.smiley_colon_e,
R.drawable.smiley_colon_o,R.drawable.smiley_colon_open,
R.drawable.smiley_colon_p,R.drawable.smiley_colon_pipe,
R.drawable.smiley_colon_s,R.drawable.smiley_colon_slash,
R.drawable.smiley_colon_star,R.drawable.smiley_colon_three,
R.drawable.smiley_colon_x,R.drawable.smiley_greater_than_colon_open,
R.drawable.smiley_kik,R.drawable.smiley_less_than_slash_three,
R.drawable.smiley_less_than_three,R.drawable.smiley_semicolon_close
};
private ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int id = Integer.parseInt(source);
Drawable draw = getResources().getDrawable(id);
draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight());
return draw;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et=(EditText) findViewById(R.id.EditText1);
expressionButton = (Button) findViewById(R.id.expressionButton);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.customdialog, null);
mGridView = (GridView) layout.findViewById(R.id.gridview);
mGridView.setAdapter(new ImageAdapter(MainActivity.this));
expressionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// MyDialog myDialog = new MyDialog(MainActivity.this);
// myDialog.show();
dialog =new Dialog(MainActivity.this);
dialog.setContentView(mGridView);
dialog.setTitle("表情选择");
dialog.show();
}
});
mGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
alertFace(position);
}
public void alertFace(int position) {
et.append(Html.fromHtml("<img src='"+face[position]+"'/>",imageGetter,null));
//e_text = et.getText().toString();
dialog.dismiss();
}
});
}
@Override
protected void onStart() {
super.onStart();
}
}
private static String TAG="MainActivity";
private Button expressionButton;
private LayoutInflater inflater;
private GridView mGridView;
private EditText et;
private Dialog dialog;
private int[]face=new int[]
{ R.drawable.smiley_b_close,R.drawable.smiley_colon_apostrophe_open,
R.drawable.smiley_colon_close,R.drawable.smiley_colon_d,
R.drawable.smiley_colon_dollar,R.drawable.smiley_colon_e,
R.drawable.smiley_colon_o,R.drawable.smiley_colon_open,
R.drawable.smiley_colon_p,R.drawable.smiley_colon_pipe,
R.drawable.smiley_colon_s,R.drawable.smiley_colon_slash,
R.drawable.smiley_colon_star,R.drawable.smiley_colon_three,
R.drawable.smiley_colon_x,R.drawable.smiley_greater_than_colon_open,
R.drawable.smiley_kik,R.drawable.smiley_less_than_slash_three,
R.drawable.smiley_less_than_three,R.drawable.smiley_semicolon_close
};
private ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int id = Integer.parseInt(source);
Drawable draw = getResources().getDrawable(id);
draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight());
return draw;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et=(EditText) findViewById(R.id.EditText1);
expressionButton = (Button) findViewById(R.id.expressionButton);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.customdialog, null);
mGridView = (GridView) layout.findViewById(R.id.gridview);
mGridView.setAdapter(new ImageAdapter(MainActivity.this));
expressionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// MyDialog myDialog = new MyDialog(MainActivity.this);
// myDialog.show();
dialog =new Dialog(MainActivity.this);
dialog.setContentView(mGridView);
dialog.setTitle("表情选择");
dialog.show();
}
});
mGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
alertFace(position);
}
public void alertFace(int position) {
et.append(Html.fromHtml("<img src='"+face[position]+"'/>",imageGetter,null));
//e_text = et.getText().toString();
dialog.dismiss();
}
});
}
@Override
protected void onStart() {
super.onStart();
}
}
解决方案 »
- linux2.6.36 下, LCD显示部分的驱动在什么地方? (想旋转一下 屏幕.)
- 写了一个service程序运行一段时间好像就自动关闭了?
- android开发环境问题
- 如何在程序中调用android自带的计算器
- 浏览器播放背景音乐的时候没有声音
- android有无深度睡眠机制??请进讨论
- 请问有没有可能将html5网页直接转成IOS/Android
- 开发一进入程序就加载地图引起ANR 怎么办呢!!
- Android FTP下载 循环获取文件异常问题
- fragment中加入高德地图,viewpager切换时地图状态复原,并且多次切换会引起程序退出
- 那位大哥帮我看看这两种写法有什么不一样
- 查看android 提供的范例的问题
// MyDialog myDialog = new MyDialog(MainActivity.this);
// myDialog.show();
dialog =new Dialog(MainActivity.this);
dialog.setContentView(mGridView);
dialog.setTitle("表情选择");
dialog.show();
}
});
感觉问题出在这里, dialog 不是局部的嘛, 为啥每次都去new呢? dialog.setContentView()这个需要?
public Drawable getDrawable(String source) {
int id = Integer.parseInt(source);
Drawable draw = getResources().getDrawable(id);
draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight());
return draw;
}
dialog =new Dialog(MainActivity.this);
dialog.setContentView(mGridView);
试试重新new个实例加进去
if(dialog!=null) {
dialog.show();
}else {
new ...
}