0

私はボタン(button1)を持っています。ボタンをクリックすると、私はcolorpickerを表示しようとしています。 BaseViewableクラスはFragmentを継承します。以下はandroid.support.v4.app.DialogFragment.showのNullpointer例外

public class Settingspreferences extends BaseViewable { 

    @Override 
    public View createView(final Context context, Object id, UserData extraData, IViewable viewable) { 

     super.createView(context, id, extraData, viewable); 


     final ColorPickerDialog colorPickerDialog = new ColorPickerDialog(); 
     colorPickerDialog.initialize(R.string.dialog_title, new int[]{Color.CYAN, Color.LTGRAY, Color.BLACK, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.RED, 
       Color.GRAY, Color.YELLOW}, Color.YELLOW, 3, 2); 

     colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() { 

      @Override 
      public void onColorSelected(int color) { 
       Toast.makeText(context, "selectedColor : " + color, Toast.LENGTH_SHORT).show(); 

      } 
     }); 


     LinearLayout list_row2 = null; 
     TextView calenderItemTV2 = null; 
     ImageView calenderItemIV2 = null; 
     Button calenderCB2 = null; 

     LinearLayout parentLL2 = (LinearLayout) root.findViewById(R.id.rowHolder3); 

       calenderCB2 = (Button) list_row2.findViewById(R.id.calenderCB2); 

       calenderCB2.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         colorPickerDialog.show(getActivity().getSupportFragmentManager(), "colorpicker"); 
        } 
       }); 
     ` 

ColorPickerDialogクラスです:

public class ColorPickerDialog extends DialogFragment implements ColorPickerSwatch.OnColorSelectedListener { 
    protected AlertDialog mAlertDialog; 
    protected int[] mColors = null; 
    protected int mColumns; 
    protected ColorPickerSwatch.OnColorSelectedListener mListener; 
    private ColorPickerPalette mPalette; 
    private ProgressBar mProgress; 
    protected int mSelectedColor; 
    protected int mSize; 
    protected int mTitleResId = R.string.color_picker_default_title; 

    private void refreshPalette() { 
     if ((this.mPalette != null) && (this.mColors != null)) 
      this.mPalette.drawPalette(this.mColors, this.mSelectedColor); 
    } 

    public void initialize(int titleId, int[] colors, int selectedColor, int columns, int size) { 
     setArguments(titleId, columns, size); 
     setColors(colors, selectedColor); 
    } 

    public void onColorSelected(int selectedColor) { 
     if (this.mListener != null) 
      this.mListener.onColorSelected(selectedColor); 
     if ((getTargetFragment() instanceof ColorPickerSwatch.OnColorSelectedListener)) 
      ((ColorPickerSwatch.OnColorSelectedListener) getTargetFragment()).onColorSelected(selectedColor); 
     if (selectedColor != this.mSelectedColor) { 
      this.mSelectedColor = selectedColor; 
      this.mPalette.drawPalette(this.mColors, this.mSelectedColor); 
     } 
     dismiss(); 
    } 

    public void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     if (getArguments() != null) { 
      this.mTitleResId = getArguments().getInt("title_id"); 
      this.mColumns = getArguments().getInt("columns"); 
      this.mSize = getArguments().getInt("size"); 
     } 
     if (bundle != null) { 
      this.mColors = bundle.getIntArray("colors"); 
      this.mSelectedColor = ((Integer) bundle.getSerializable("selected_color")).intValue(); 
     } 
    } 

    public Dialog onCreateDialog(Bundle bundle) { 
     View view = LayoutInflater.from(getActivity()).inflate(R.layout.color_picker_dialog, null); 
     this.mProgress = ((ProgressBar) view.findViewById(android.R.id.progress)); 
     this.mPalette = ((ColorPickerPalette) view.findViewById(R.id.color_picker)); 
     this.mPalette.init(this.mSize, this.mColumns, this); 
     if (this.mColors != null) 
      showPaletteView(); 
     this.mAlertDialog = new AlertDialog.Builder(getActivity()).setTitle(this.mTitleResId).setView(view).create(); 
     return this.mAlertDialog; 
    } 

    public void onSaveInstanceState(Bundle bundle) { 
     super.onSaveInstanceState(bundle); 
     bundle.putIntArray("colors", this.mColors); 
     bundle.putSerializable("selected_color", Integer.valueOf(this.mSelectedColor)); 
    } 

    public void setArguments(int titleId, int columns, int size) { 
     Bundle bundle = new Bundle(); 
     bundle.putInt("title_id", titleId); 
     bundle.putInt("columns", columns); 
     bundle.putInt("size", size); 
     setArguments(bundle); 
    } 

    public void setColors(int[] colors, int selected) { 
     if ((this.mColors != colors) || (this.mSelectedColor != selected)) { 
      this.mColors = colors; 
      this.mSelectedColor = selected; 
      refreshPalette(); 
     } 
    } 

    public void setOnColorSelectedListener(ColorPickerSwatch.OnColorSelectedListener onColorSelectedListener) { 
     this.mListener = onColorSelectedListener; 
    } 

    public void showPaletteView() { 
     if ((this.mProgress != null) && (this.mPalette != null)) { 
      this.mProgress.setVisibility(View.GONE); 
      refreshPalette(); 
      this.mPalette.setVisibility(View.VISIBLE); 
     } 
    } 

    public void showProgressBarView() { 
     if ((this.mProgress != null) && (this.mPalette != null)) { 
      this.mProgress.setVisibility(View.VISIBLE); 
      this.mPalette.setVisibility(View.GONE); 
     } 
    } 
} 

私は同じ問題に似ているが、解決できませんでした多くのポストを見てきましたが、私は、問題をそれを把握することはできませんよ。

07-25 14:44:54.113 25526-25526/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference 
07-25 14:44:54.113 25526-25526/? W/System.err:  at android.support.v4.app.DialogFragment.show(DialogFragment.java:137) 
07-25 14:44:54.113 25526-25526/? W/System.err:  at com.sap.rex.ui.CalendarSettingspreferences$2.onClick(CalendarSettingspreferences.java:258) 
07-25 14:44:54.113 25526-25526/? W/System.err:  at android.view.View.performClick(View.java:5246) 
07-25 14:44:54.113 25526-25526/? W/System.err:  at android.widget.TextView.performClick(TextView.java:10573) 
07-25 14:44:54.113 25526-25526/? W/System.err:  at android.view.View$PerformClick.run(View.java:21256) 
07-25 14:44:54.113 25526-25526/? W/System.err:  at android.os.Handler.handleCallback(Handler.java:739) 
07-25 14:44:54.113 25526-25526/? W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:95) 
07-25 14:44:54.113 25526-25526/? W/System.err:  at android.os.Looper.loop(Looper.java:145) 
07-25 14:44:54.113 25526-25526/? W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:6912) 
07-25 14:44:54.113 25526-25526/? W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
07-25 14:44:54.113 25526-25526/? W/System.err:  at java.lang.reflect.Method.invoke(Method.java:372) 
07-25 14:44:54.113 25526-25526/? W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
07-25 14:44:54.113 25526-25526/? W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
+0

がメインページ –

+0

を追加し、私は – lal

+0

@AkshayからもごcolorPickerDialogの初期化コードを表示してください、あなたのクラッシュログ – user6434454

答えて

0

それはandroid.support.v4.app.DialogFragmentですので、ダイアログの断片を示すためgetSupportFragmentManager()を使用する必要があります。

は私のログをチェックしてください助けてください。

アクティビティから表示されている場合:

colorPickerDialog.show(getSupportFragmentManager(), "colorpicker");

を使用すると、フラグメントから表示されている場合:

colorPickerDialog.show(getActivity().getSupportFragmentManager(), "colorpicker");

編集1: 使用

colorPickerDialog.show(getChildFragmentManager(), "colorpicker");

+0

を確認してください、私はこれを使ってみましたが、didnの自分のクラスがfragmentActivityを拡張せず、代わりにフラグメントを拡張します – user6434454

+0

あなたのアクティビティは 'FragmentActivity'または' AppCompatActivity'を拡張する必要があります。これらのアクティビティを拡張せずに 'getSupportFragmentManager'を使用する他の方法はありません。 –

+0

私のクラスはすでに抽象クラスで拡張されており、抽象クラスはフラグメントに拡張されており、2つのクラスを拡張することはできません。何をすべきか? – user6434454

関連する問題