2017-03-23 13 views
0

私はこのライブラリを使用しました:https://github.com/kristiyanP/colorpicker私のアプリにカラーピッカーを持たせるには、実装した後に複数回使用できません。私は、カラーピッカーを開こう:私はここに投稿する前に検索が、どのような方法でこのエラーを修正couldntのフラグメントで2回目のダイアログを膨張させる際にエラーが発生しました

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); 
} 
} 

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。 ありがとうございます。

答えて

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(); 
} 
関連する問題