1

据我所知如果在旧版本中使用 datepicker 到 android 3.0 我需要扩展 `FragmentActivity 而不是 Activity 。如果我运行这个应用程序,它会强制关闭。

在该站点中提到我们需要使用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
4

1 回答 1

0

看来您混合了来自操作系统和支持库的类。检查您的导入以确保您只使用来自支持库的片段相关类,例如android.support.v4.app.FragmentActivity.

于 2012-12-25T09:27:17.920 回答