为什么DialogFragments在Android 2.3.3中呈现forceclose?

时间:2012-12-25 09:02:58

标签: android datepicker

当我通过this了解到我需要扩展`FragmentActivity而不是Activity,如果 datepicker 在旧版本中用于android 3.0。如果我运行此应用程序,它会强行关闭。

在网站中提到我们需要使用getSupportFragmentManager()而不是getFragmentManager()。如果使用getFragmentManager(),我的应用会崩溃,如果我使用getSupportFragmentManager()则会显示错误。任何帮助,将不胜感激。在此先感谢我的代码。

public class MainActivity extends FragmentActivity {

     static EditText edit = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit = (EditText)findViewById(R.id.editText1);

        edit.setOnClickListener(new OnClickListener() {

            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datePicker");   

            }
        });
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
        StringBuffer sb = new StringBuffer();
        sb.append(year).append("/").append(month).append("/").append(day);
        edit.setText(sb);

    }
}
}

日志文件已编辑...

12-25 14:23:26.575: E/AndroidRuntime(443): FATAL EXCEPTION: main
12-25 14:23:26.575: E/AndroidRuntime(443): java.lang.NoClassDefFoundError: com.example.datetestapp.MainActivity$DatePickerFragment
12-25 14:23:26.575: E/AndroidRuntime(443):  at com.example.datetestapp.MainActivity$1.onClick(MainActivity.java:33)
12-25 14:23:26.575: E/AndroidRuntime(443):  at android.view.View.performClick(View.java:2485)
12-25 14:23:26.575: E/AndroidRuntime(443):  at android.view.View$PerformClick.run(View.java:9080)
12-25 14:23:26.575: E/AndroidRuntime(443):  at android.os.Handler.handleCallback(Handler.java:587)
12-25 14:23:26.575: E/AndroidRuntime(443):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-25 14:23:26.575: E/AndroidRuntime(443):  at android.os.Looper.loop(Looper.java:123)
12-25 14:23:26.575: E/AndroidRuntime(443):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-25 14:23:26.575: E/AndroidRuntime(443):  at java.lang.reflect.Method.invokeNative(Native Method)
12-25 14:23:26.575: E/AndroidRuntime(443):  at java.lang.reflect.Method.invoke(Method.java:507)
12-25 14:23:26.575: E/AndroidRuntime(443):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-25 14:23:26.575: E/AndroidRuntime(443):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-25 14:23:26.575: E/AndroidRuntime(443):  at dalvik.system.NativeStart.main(Native Method)
12-25 14:23:28.654: I/Process(443): Sending signal. PID: 443 SIG: 9

1 个答案:

答案 0 :(得分:0)

您似乎从操作系统和支持库中获得了混合类。检查导入以确保只使用支持库中的片段相关类,如android.support.v4.app.FragmentActivity