2013年9月10日 星期二

java.lang.IllegalStateException:The specifiedchildalready has a parent. Youmust call removeView() on the child's parent first

public class HotkeyAppListDialog extends DialogFragment {

    public static HotkeyAppListDialog newInstance(int position) {
        HotkeyAppListDialog frag = new HotkeyAppListDialog();
        return frag;
    }
   
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        HotkeyAppListAdapter adapter = new HotkeyAppListAdapter(getActivity(), R.layout.hotkey_app_list_row);
---------------------------------------------------------------------
Where causes the exception !

        View list = getActivity().getLayoutInflater().inflate(R.layout.hotkey_app_list, null);                    
        ListView appList = (ListView) list.findViewById(R.id.app_list_view);
The correct way to create a ListView instance here to avoid re-attach parent !

        ListView appList = new ListView(getActivity());    
---------------------------------------------------------------------
        appList.setAdapter(adapter);   
        builder.setView(appList);
        builder.setTitle(R.string.hotkey_app_list_title);  
        return builder.create();
    }
}

public class HotkeySettings extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {                    
        View root = inflater.inflate(R.layout.hotkey_main, container, false);                    
        ListView list = (ListView) root.findViewById(android.R.id.list);
        HotkeyListAdapter hla = new HotkeyListAdapter(getActivity(), R.layout.hotkey_list_row);   
        list.setAdapter(hla);
        return root;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        DialogFragment had = HotkeyAppListDialog.newInstance(position);
        had.show(getActivity().getFragmentManager(), "hotkey app list");   
    }    
}

Reference links
http://stackoverflow.com/questions/13504781/custom-listview-inside-a-dialog-in-android

沒有留言:

張貼留言