第二次在片段上错误地膨胀对话框

时间:2017-03-23 18:16:00

标签: android android-fragments android-alertdialog

我使用这个库:https://github.com/kristiyanP/colorpicker为了在我的应用程序上有一个颜色选择器,在实现它之后我不能使用它多次,它第二次尝试打开时会出现以下错误颜色选择器:

FATAL EXCEPTION: main
                                                                         Process: com.example.mobiware.ivaapp, PID: 14147
                                                                         java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                             at android.view.ViewGroup.addViewInner(ViewGroup.java:4656)
                                                                             at android.view.ViewGroup.addView(ViewGroup.java:4497)
                                                                             at android.view.ViewGroup.addView(ViewGroup.java:4437)
                                                                             at android.view.ViewGroup.addView(ViewGroup.java:4410)
                                                                             at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:279)
                                                                             at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:88)
                                                                             at petrov.kristiyan.colorpicker.CustomDialog.onCreate(CustomDialog.java:20)
                                                                             at android.app.Dialog.dispatchOnCreate(Dialog.java:759)
                                                                             at android.app.Dialog.show(Dialog.java:486)
                                                                             at petrov.kristiyan.colorpicker.ColorPicker.show(ColorPicker.java:225)
                                                                             at com.example.mobiware.ivaapp.Activities.AddCategory$1.onClick(AddCategory.java:40)
                                                                             at android.view.View.performClick(View.java:6207)
                                                                             at android.widget.TextView.performClick(TextView.java:11094)
                                                                             at android.view.View$PerformClick.run(View.java:23639)
                                                                             at android.os.Handler.handleCallback(Handler.java:751)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

CODE

public class fragment_category extends Fragment {

private SQLiteHandler db;
Spinner categoriesspinner;
ColorPicker colorPicker;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View RootView = inflater.inflate(R.layout.fragment_addcategory, container, false);

    db = new SQLiteHandler(getApplicationContext());
    categoriesspinner = (Spinner) RootView.findViewById(R.id.spinnercategory);

    colorPicker = new ColorPicker(getActivity());

    Button color = (Button) RootView.findViewById(R.id.color);
    color.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            colorPicker.show();
        }
    });

    return RootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getActivity().setTitle("Adicionar categoria");

    colorPicker.setOnChooseColorListener(new ColorPicker.OnChooseColorListener() {
        @Override
        public void onChooseColor(int position, int color) {
            Log.d("color:", String.valueOf(color));
            Log.d("color:", String.valueOf(position));
        }

        @Override
        public void onCancel(){
        }
    });

    loadCategoriesSpinner();
}

public void loadCategoriesSpinner() {
    db = new SQLiteHandler(getApplicationContext());

    List<String> categories = db.getAllCategory();

    ArrayAdapter<String> dataAdapterCategories = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, categories);

    dataAdapterCategories.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    categoriesspinner.setAdapter(dataAdapterCategories);
}
 }

我在发布之前搜索过但无法以任何方式修复此错误。 谢谢。

1 个答案:

答案 0 :(得分:0)

您是否有可能不止一次(每次点击)调用show()并且颜色选择器不支持它?

如果这是问题,您可以声明为每次点击创建一个新的ColorPicker。例如:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View RootView = inflater.inflate(R.layout.fragment_addcategory, container, false);

    db = new SQLiteHandler(getApplicationContext());
    categoriesspinner = (Spinner) RootView.findViewById(R.id.spinnercategory);

    Button color = (Button) RootView.findViewById(R.id.color);
    color.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            colorPicker = new ColorPicker(getActivity());
            colorPicker.show();
        }
    });

    return RootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getActivity().setTitle("Adicionar categoria");

    if (colorPicker != null) {
        colorPicker.setOnChooseColorListener(new ColorPicker.OnChooseColorListener() {
            @Override
            public void onChooseColor(int position, int color) {
                Log.d("color:", String.valueOf(color));
                Log.d("color:", String.valueOf(position));
            }

            @Override
            public void onCancel() {
            }
        });
    }

    loadCategoriesSpinner();
}