0

我有一个按钮(button1)。点击按钮,我试图显示colorpicker。 BaseViewable类扩展Fragment。android.support.v4.app.DialogFragment.show中的空指针异常

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

也请出示colorPickerDialog初始化代码从炫魅 – lal

+0

@Akshay,我已经贴我的logcat,请 – user6434454

回答

0

既然是android.support.v4.app.DialogFragment,你应该使用getSupportFragmentManager()用于显示对话片段。

如果从活动显示:

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

编辑1:

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

如果从片段显示 使用

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

+0

我试图用这个,但没不工作,因为我的类不会扩展fragmentActivity,而是扩展片段 – user6434454

+0

您的活动应该扩展'FragmentActivity'或'AppCompatActivity'。没有其他方法可以使用'getSupportFragmentManager'而不扩展这些活动。 –

+0

我的类已经扩展了一个抽象类,抽象类扩展为fragment,并且不可能扩展2个类。该怎么办? – user6434454