private static final int CREATE_LOCATION_DIALOG=1;
         private static final int  SEARCH_LOCATION_DIALOG=2;
         private static final int CONFIRM_DELETE_DIALOG = 3;
         private static final int CONFIRM_EDIT_DIALOG = 4;
         private static final int LOCATION_NAME_IS_EMPTY=31; 
         private static final int LOCATION_NAME_IS_EXISTED=32;
        
        private ArrayAdapter<String> locationListAdapter = null;
         private View textEntryView = null;
         private Dialog createLocationDialog = null;
         private Dialog confirmDeleteDialog = null;
         private Dialog editLocationDialog=null;
         private Integer positionOfItem = null;@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case CREATE_LOCATION_DIALOG:
if(createLocationDialog==null){
AlertDialog.Builder builder=new AlertDialog.Builder(this);builder.setTitle(R.string.add);
builder.setView(getTextEntryView());builder.setCancelable(true);
builder.setPositiveButton(R.string.save, addLocationButtonListener());
builder.setNegativeButton(R.string.cancel, cancelListener());
createLocationDialog=builder.create();
}return createLocationDialog;
case CONFIRM_DELETE_DIALOG:
if(confirmDeleteDialog==null){
AlertDialog.Builder builder=new AlertDialog.Builder(this);builder.setMessage("确定要删除该地点么?");
builder.setCancelable(true);
builder.setPositiveButton(R.string.yes, deleteLocationButtonListener());
builder.setNegativeButton(R.string.no, cancelListener());
confirmDeleteDialog=builder.create();
}
return confirmDeleteDialog;
case CONFIRM_EDIT_DIALOG:
if(editLocationDialog==null){
AlertDialog.Builder builder=new AlertDialog.Builder(this);builder.setTitle(R.string.edit);builder.setView(getTextEntryView());builder.setCancelable(true);
builder.setPositiveButton(R.string.save, editLocationButtonListener());
builder.setNegativeButton(R.string.cancel, cancelListener());
editLocationDialog=builder.create();
}
return editLocationDialog;
default:
Logger.e("Attempting to create an unkonwn dialog with an id of " + id);
return null;
}
} protected DialogInterface.OnClickListener addLocationButtonListener() {
             return new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                 EditText collectedTextView = (EditText) getTextEntryView().findViewById(R.id.collected_locationlist_edittext);//输入框
                 String name = collectedTextView.getText().toString();
                 
                 if (name.isEmpty()||name==null){
                         Logger.d("Called when the name is empty");
                         DisplayToast(LOCATION_NAME_IS_EMPTY);
               
                 }
                 else if(Location.checkTheLocationName(name, LocationList.this)){
                         Logger.d("Called when the name is already existed");
                         DisplayToast(LOCATION_NAME_IS_EXISTED);
                 }
                 else{
                 Logger.d("Add Location Successfully");
                 Location.saveLocation(name, LocationList.this);
                 locationListAdapter.add(name);
                 //removeDialog(CREATE_LOCATION_DIALOG);//清除缓存
                 
                 
                 }
             }
         };
        }
  //编辑按钮监听
    protected DialogInterface.OnClickListener editLocationButtonListener() {
            return new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                EditText collectedTextView = (EditText) getTextEntryView().findViewById(R.id.collected_locationlist_edittext);//输入框
                String name = collectedTextView.getText().toString();
                
                if (name.isEmpty()||name==null){
                        Logger.d("Called when the name is empty");
                        DisplayToast(LOCATION_NAME_IS_EMPTY);
               
                }
                else if(Location.checkTheLocationName(name, LocationList.this)){
                        Logger.d("Called when the name is already existed");
                        DisplayToast(LOCATION_NAME_IS_EXISTED);
                }
                else{
                Logger.d("Edit this Location Successfully");
                Location.editLocation(name, LocationList.this);
                locationListAdapter.add(name);
                //removeDialog(CONFIRM_EDIT_DIALOG);//清除缓存
                
                }
            }
        };
        } synchronized protected View getTextEntryView() {
             if (textEntryView == null) {
            LayoutInflater factory = LayoutInflater.from(this);
        textEntryView = factory.inflate(R.layout.collected_locationlist_text, null);
             }
             
             else{
             
                 Logger.d("textEntryView is not null");
         }
            return textEntryView;
      
    }collected_locationlist_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    ><EditText         android:id="@+id/collected_locationlist_edittext"
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:padding="5dp" /></LinearLayout>以上就是相关的代码,想实现的是添加编辑及删除
这三项都是以AlertDialog方式展现的,其中添加与编辑是同样的AlertDialog,都带一个输入框,只是title不同问题主要出现在编辑于添加上,当我添加一个后,然后点击编辑就会出错,错误ERROR/AndroidRuntime(1394): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
这个问题我查了一个晚上,网上讲的我试了一些,
应该是第一次创建了dialog,第二次使用的是编辑,但是还是同一个dialog
其实准确讲,应该是在builder.setview(getTextEntryView())这里出错的
因为我 注释掉之后,就不会有错,但是当然也不会有输入框了至于错误提示的removeView(),我也试了下,不知道是我弄错还是如何,总之错误依旧
现在我是没辙了,当然也可以用两个独立的layout文件,但是我觉得那样虽然看上去差不多,但是如果我做更多的功能就要每一个都弄,不太好
求教解决方法,在此谢过

解决方案 »

  1.   

    去掉textEntryView成员变量,把getTextEntryView修改如下:synchronized protected View getTextEntryView() {
        LayoutInflater factory = LayoutInflater.from(this);
        return factory.inflate(R.layout.collected_locationlist_text, null);
    }
      

  2.   


    多谢回复,我照您说的做了,暂时好像是没问题,但是产生了新的一个问题
    就是输入框输入的名字无法保存下来也就是添加按钮监听那里 public void onClick(DialogInterface dialog, int id) {
                     EditText collectedTextView = (EditText) getTextEntryView().findViewById(R.id.collected_locationlist_edittext);//输入框
                     String name = collectedTextView.getText().toString();
                     我在这里打了个Logger输出,显示这个name为空的
    很费解