android - 使用 DatePickerDialog 时冲突 support.v4.app.Fragment 与 app.Fragment

标签 android android-fragments navigation-drawer android-support-library datepickerdialog

我不太明白 support.v4 库是怎么回事,但无论如何。我有一个 fragment ,我实现了在单击抽屉导航中的项目时打开(android studio 的默认实现)。在这个 fragment 中,我想打开一个 datePickerDialog 但我找不到让它工作的解决方案。这是我的 datePicker fragment :

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle; 
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.Toast;

import java.util.Calendar;

public class SearchFragment extends Fragment {

private ListView listView;
private CustomAdapter activityAdapter;
private Button filterButton;

public SearchFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_search, container, false);

    activityAdapter = new CustomAdapter(getActivity());

    listView = (ListView) rootView.findViewById(R.id.lvActivity);
    listView.setAdapter(activityAdapter);
    activityAdapter.loadObjects();
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            String activity = activityAdapter.getItem(position).get("activity").toString();
            Toast.makeText(getActivity(), activity, Toast.LENGTH_SHORT).show();
        }
    });
    filterButton = (Button) rootView.findViewById(R.id.bFilter);

    return rootView;
}

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

public static class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    @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
    }
}

我的问题出在这一行:newFragment.show(getSupportFragmentManager(), "datePicker");

我发现如果 Activity 没有扩展 Fragment 但我的 Activity 已经在扩展 ActionBarActivity 就会出现兼容性问题。我该怎么办?

最佳答案

不,你没有。你在这里导入错误

  import android.app.DialogFragment;

更改它以使用支持库

关于android - 使用 DatePickerDialog 时冲突 support.v4.app.Fragment 与 app.Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24905481/

相关文章:

android - android应用程序的渗透测试技术是什么?

android - 如何从子 Activity 而不是主 Activity 使用 facebook 登录

android - onOptionsItemSelected不能在 fragment 中工作

android - FrameLayout 中的 fragment 未显示在 CoordinatorLayout 中

Android WebView 安全字体系列?

Android 无法在 Assets 文件夹中找到文件

android - 如何在 Android 中加密 session 值

android - 使用 FragmentStatePagerAdapater "The specified child already has a parent"错误

ios - 带侧面菜单的页面查看器(抽屉导航)

android - 打开另一个 Activity 后关闭抽屉导航