我正在使用DatePickerFragment类将数据设置为textView。
该班上课了,但是当我打开屏幕时,应用程序崩溃了。

DataPicker类的代码:

class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
    public TextView textview_editTextVnosDatuma;
    public DatePickerFragment(TextView elementVKateregaSeShraniDatum){
        textview_editTextVnosDatuma = elementVKateregaSeShraniDatum;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        final Calendar C = Calendar.getInstance();
        int y = C.get(Calendar.YEAR);
        int m = C.get(Calendar.MONTH);
        int d = C.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, y, m, d);
    }

    public void onDateSet(DatePicker view, int year, int month, int day){
        textview_editTextVnosDatuma.setText(""+year+"-"+(month+1)+"-"+day);
    }
}




我正在从单击文本视图开始的方法(在“活动”中)调用类:

public void SetTheData(View view){
    new DatePickerFragment(((TextView) findViewById(R.id.editTextInsertData))).show(getSupportFragmentManager(), "datePicker");
}




这是LogCat中的错误:

07-09 10:52:32.140: W/dalvikvm(5748): threadid=1: thread exiting with uncaught exception (group=0x40bfb1f8)
07-09 10:52:32.171: E/AndroidRuntime(5748): FATAL EXCEPTION: main
07-09 10:52:32.171: E/AndroidRuntime(5748): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.miniMAX.mmcitalnik/com.miniMAX.mmcitalnik.VnosPrejemaIzdaje}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.miniMAX.mmcitalnik.DatePickerFragment: make sure class name exists, is public, and has an empty constructor that is public
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3365)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.app.ActivityThread.access$700(ActivityThread.java:128)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1165)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.os.Looper.loop(Looper.java:137)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.app.ActivityThread.main(ActivityThread.java:4514)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at java.lang.reflect.Method.invoke(Method.java:511)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at dalvik.system.NativeStart.main(Native Method)
07-09 10:52:32.171: E/AndroidRuntime(5748): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.miniMAX.mmcitalnik.DatePickerFragment: make sure class name exists, is public, and has an empty constructor that is public
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.support.v4.app.Fragment.instantiate(Fragment.java:405)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at com.miniMAX.mmcitalnik.VnosPrejemaIzdaje.onCreate(VnosPrejemaIzdaje.java:57)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.app.Activity.performCreate(Activity.java:4465)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-09 10:52:32.171: E/AndroidRuntime(5748):     ... 12 more
07-09 10:52:32.171: E/AndroidRuntime(5748): Caused by: java.lang.InstantiationException: can't instantiate class com.miniMAX.mmcitalnik.DatePickerFragment; no empty constructor
07-09 10:52:32.171: E/AndroidRuntime(5748):     at java.lang.Class.newInstanceImpl(Native Method)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at java.lang.Class.newInstance(Class.java:1319)
07-09 10:52:32.171: E/AndroidRuntime(5748):     at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
07-09 10:52:32.171: E/AndroidRuntime(5748):     ... 19 more



我怎么解决这个问题?

最佳答案

将您的DatePickerFragment类公开,并提供一个空的公共构造函数。

10-06 09:16