Android logcat:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
W/System.err: at android.view.ViewGroup.addViewInner(ViewGroup.java:4168)
W/System.err: at android.view.ViewGroup.addView(ViewGroup.java:4018)
W/System.err: at com.android.internal.app.AlertController.setupTitle(AlertController.java:547)
W/System.err: at com.android.internal.app.AlertController.setupView(AlertController.java:474)
W/System.err: at com.android.internal.app.AlertController.installContent(AlertController.java:240)
W/System.err: at android.app.AlertDialog.onCreate(AlertDialog.java:356)
W/System.err: at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
W/System.err: at android.app.Dialog.show(Dialog.java:274)
W/System.err: at SingleView.showDialog(MyActivity.java:201)
W/System.err: at MyActivity$4.onItemClick(MyActivity.java:354)
W/System.err: at MyAdapter$MyViewHolder.onClick(MyAdapter.java:47)
W/System.err: at android.view.View.performClick(View.java:5076)
W/System.err: at android.view.View$PerformClick.run(View.java:20279)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:135)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5910)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
在MyActivity中,201行是:
alert.show();
第354行
showDialog();
在MyAdapter第47行中是:
mOnMyItemClickListener.onItemClick(v, getAdapterPosition());
这是MyAdapter类:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter .MyViewHolder>{
public interface OnMyItemClickListener{
void onItemClick(View v, int position);
}
private OnMyItemClickListener mOnMyItemClickListener;
private List<String> customTitles;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.amenities_card, parent, false);
return new MyViewHolder(itemView);
}
public MyAdapter( List<String> customTitles, OnMyItemClickListener OnMyItemClickListener) {//Context context,
//this.mContext = context;
this.placeTitles = customTitles;
mOnMyItemClickListener = OnMyItemClickListener;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.textName.setText(customTitles.get(position));
}
@Override
public int getItemCount() {
return customTitles.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView textName;
public MyViewHolder(final View view) {
super(view);
placeName = (TextView) view.findViewById(R.id.customTitle);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mOnMyItemClickListener.onItemClick(v, getAdapterPosition());
}
}
}
In MyActivity, this is the adapter's onClickListener:
MyAdapter aAdapter = new AmenitiesAdapter(customTitles, new MyAdapter.OnMyItemClickListener(){
@Override
public void onItemClick(View v, int position) {
//your code goes here
final TextView textName = (TextView) v.findViewById(R.id.customTitle);
String text1= textName.getText().toString();
String[] mText1= v.getRootView().getContext().getResources().getStringArray(R.array.text_1);
String type = mText1[position];
//Toast.makeText(MyActivity.this, text1, Toast.LENGTH_LONG).show();
//Toast.makeText(MyActivity.this, "At position"+position, Toast.LENGTH_LONG).show();
try {
//
}
catch(NullPointerException e){
e.printStackTrace();
}
try {
showDialog();
}
catch (IllegalStateException e){
e.printStackTrace();
}
}
});
这是显示对话框的方法:
public void showDialog(){
title.setText(R.string.title_text);
// title.setBackgroundColor(Color.DKGRAY);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(18);
final AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.DialogStyle);
builder.setCustomTitle(title)
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// dialog.dismiss();
Toast.makeText(SingleView.this,"my text",Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("NOTHING", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alert = builder.create();
alert.show();
}
这是onBackPressed方法:
@Override
public void onBackPressed(){
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer != null && slidingDrawer!=null){
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
else if (slidingDrawer.isOpened()) {
slidingDrawer.close();
}
else {
if(i.getStringExtra("prev_activity").equals("Main")){
Intent intent = new Intent(MyActivity.this, MainActivity.class);
startActivity(intent);
}
if(alert.isShowing()){alert.hide();alert.cancel();alert.dismiss();}
super.onBackPressed();
}
}
}
情况如此:我的回收站视图位于滑动抽屉中。 当我单击recyclerview中的某个项目时,会显示一个警告对话框,如果我通过按后退按钮关闭弹出窗口,然后单击recyclerview项目,则警告对话框不显示,应用程序崩溃并给出IllegalState异常。请帮忙。
如果解雇对话,我可以问你如何销毁对话框吗?我尝试删除所有子元素,因为我认为它是使用
的唯一子元素((ViewGroup) linearlayout.getParent()).removeAllViews();
但是当我使用
检查时((ViewGroup) linearlayout.getParent()).removeViews(0, ll.getChildCount());
我发现有9个子视图,是否显示警告对话框。如何完全销毁警报对话框?
我也尝试过:
((ViewGroup)ll.getParent())。removeView(alert.getWindow()。getDecorView()); ((ViewGroup)ll.getParent())。removeView(alert.getWindow()。peekDecorView());
但例外仍在抛出。