0

사용자가 0에서 10 사이의 값을 선택할 수 있도록하기 위해 NumberPicker를 대화 상자에 표시하려고합니다. 작동시키기 위해 노력한 두 번째 날입니다.DialogFragment 및 NumberPicker의 InvocationTargetException

  • fragment_number_picker (레이아웃) :

    <?xml version="1.0" encoding="utf-8"?> 
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
         android:id="@+id/numberPickerFragmentLinearLayout" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:gravity="center" 
         android:orientation="horizontal" > 
    
        <NumberPicker 
         android:id="@+id/numberPickerInFragment" 
         android:layout_width="wrap_content" 
         android:layout_height="48dp" 
         android:orientation="horizontal" /> 
    
        </LinearLayout> 
    
  • DialogFragment 정의 :

    : 주요 활동에서

    public class NumberPickerCustomDialog extends DialogFragment { 
    Context context; 
    public NumberPickerCustomDialog() {} 
    
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
        context = getActivity().getApplicationContext(); 
        AlertDialog.Builder builder = new AlertDialog.Builder(context); 
        LayoutInflater li = (LayoutInflater)  
         context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    
        // Inflate and set the layout for the dialog 
        View view = li.inflate(R.layout.fragment_number_picker, null); 
    
        builder 
        // Set view: 
         .setView(view) 
        // Add action buttons 
         .setPositiveButton(R.string.accept, new DialogInterface.OnClickListener(){ 
          @Override 
          public void onClick(DialogInterface dialog, int id) { 
            // Accept number 
          } 
         }) 
         .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){ 
          public void onClick(DialogInterface dialog, int id) { 
           dialog.dismiss(); 
          } 
         }); 
    
        NumberPicker np = (NumberPicker) view.findViewById(R.id.numberPickerInFragment); 
        np.setMaxValue(200); 
        np.setMinValue(0); 
    
        np.setFocusable(true); 
        np.setFocusableInTouchMode(true); 
    
        return builder.create(); 
        } 
    } 
    
  • 전화

    내가 가진 무엇
    public void openNumberPicker(){ 
        FragmentManager fm = getSupportFragmentManager(); 
        NumberPickerCustomDialog npc = new NumberPickerCustomDialog(); 
        npc.show(fm, "fragment_number_picker"); 
    } 
    

InvocationTargetException이 표시되어 작동하지 않습니다.

아이디어가 있으십니까? 감사!

답변

0

사용하고있는 상황 변경해보십시오 :

context = getActivity(); 
대신

context = getActivity().getApplicationContext(); 

을, 당신은 활동의 자신의 상황에 맞는 필요