安卓重启日历

标签 android android-fragments android-calendar

我把这个网站日历 https://github.com/roomorama/Caldroid

成功挂载到fragments中

但是在我替换 fragment 之后

一切都会变成空白日历。

我必须更换才能正确显示月份。

但是我更改指定日期的颜色失败。

调用格式数据会引发空指针异常。

我该如何修复。

Activity

public class index_Activity extends FragmentActivity{
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
            fm2 = getSupportFragmentManager();
            DSE2 = fm2.beginTransaction();
            e_f=fm2.findFragmentById(R.id.main_frame_e);

            mRadioButton1.toggle();


        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            Log.i("zj", "checkedid="+checkedId);
            fm2 = getSupportFragmentManager();
            DSE2 = fm2.beginTransaction();
            DF2 = fm2.findFragmentById(R.id.main_frame);

            if (checkedId == R.id.btn1) {

                if (DF2 == null ) {
                    fg_a usermune = new fg_a("nono");
                    DSE2.add(R.id.main_frame,usermune);
                    DSE2.commit();
                } else if (DF2 != null) {
                    DSE2.remove(DF2);
                    fg_a usermune = new fg_a("nono");
                    DSE2.add(R.id.main_frame,usermune);
                    DSE2.commit();
                }

            }else if (checkedId == R.id.btn2) {
                if (DF2== null ) {
                    fg_b usermune = new fg_b("nono");
                    DSE2.add(R.id.main_frame, usermune);
                    DSE2.commit();
                } else if (DF2 != null ) {
                    DSE2.remove(DF2);
                    fg_b usermune = new fg_b("nono");
                    DSE2.add(R.id.main_frame,usermune);
                    DSE2.commit();
                }

            }else if (checkedId == R.id.btn3) {
    ///////////////////Open the calendar//
                if (DF2== null ) {
                    fg_c usermune = new fg_c("nono");
                    DSE2.add(R.id.main_frame, usermune);
                    DSE2.commit();
                } else if (DF2 != null ) {
                    DSE2.remove(DF2);
                    fg_c usermune = new fg_c("nono");
                    DSE2.add(R.id.main_frame, usermune);
                    DSE2.commit();
                }
    ////////////////////////////////////////

            }else if (checkedId == R.id.btn4) {
                page[i]=4;
                i++;
         startActivity(fg_d_link);


                }else if (checkedId == R.id.btn5) {
                touch=4;
                page[i]=5;
                i++;
                if (DF2== null ) {
                    fg_e usermune = new fg_e("nono");
                    DSE2.add(R.id.main_frame, usermune);
                    DSE2.commit();
                } else if (DF2 != null ) {
                    DSE2.remove(DF2);
                    fg_e usermune = new fg_e("nono");
                    DSE2.add(R.id.main_frame, usermune);
                    DSE2.commit();
                }

            }

            mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();

        }

日历 fragment :

public class fg_c extends Fragment {
    SimpleDateFormat formatter;
    Date[] setday;
    int t;
    FragmentManager fm;
    FragmentTransaction DSE2;
    Fragment DF2;
    private DBOpenHelper dbHelper;
    private ArrayList<Site> sites;
    private final String title1;
    int page_open=0;
    public fg_c(String string) {
        this.title1 = string;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View fragmentView = inflater.inflate(R.layout.caldroid_main,container, false);

   formatter = new SimpleDateFormat("dd MMM yyyy");

        // Setup caldroid fragment
        // **** If you want normal CaldroidFragment, use below line ****
        caldroidFragment = new CaldroidFragment();







        // //////////////////////////////////////////////////////////////////////
        // **** This is to show customized fragment. If you want customized
        // version, uncomment below line ****
        // caldroidFragment = new CaldroidSampleCustomFragment();

        // Setup arguments

        // If Activity is created after rotation
        if (savedInstanceState != null) {
            caldroidFragment.restoreStatesFromKey(savedInstanceState,
                    "CALDROID_SAVED_STATE");
        }
        // If activity is created from fresh
        else {
            Bundle args = new Bundle();
            Calendar cal = Calendar.getInstance();
            args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
            args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
            args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
            args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, true);
            caldroidFragment.setArguments(args);
        }

        setCustomResourceForDates();

        // Attach to the activity
        android.support.v4.app.FragmentTransaction t =      getActivity().getSupportFragmentManager().beginTransaction();
        t.replace(R.id.calendar1, caldroidFragment);
        t.commit();

        // Setup listener
        final CaldroidListener listener = new CaldroidListener() {

            @Override
            public void onSelectDate(Date date, View view) {
//nullpointerexception
                Toast.makeText(getActivity(), formatter.format(date),
                        Toast.LENGTH_SHORT).show();
//

            }

            @Override
            public void onChangeMonth(int month, int year) {
                String text = "month: " + month + " year: " + year;
                Toast.makeText(getActivity(), text,
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onLongClickDate(Date date, View view) {
                Toast.makeText(getActivity(),
                        "Long click " + formatter.format(date),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCaldroidViewCreated() {
                if (caldroidFragment.getLeftArrowButton() != null) {
                    Toast.makeText(getActivity(),
                            "Caldroid view is created", Toast.LENGTH_SHORT)
                            .show();
                }
            }


        };

        // Setup Caldroid
        caldroidFragment.setCaldroidListener(listener);

        final TextView textView = (TextView) fragmentView.findViewById(R.id.textview);
        return fragmentView;
    }
    private boolean undo = false;
    private CaldroidFragment caldroidFragment;
    private CaldroidFragment dialogCaldroidFragment;

    private void setCustomResourceForDates() {
        Calendar cal = Calendar.getInstance();

        for(int i=0;i<t;i++){
            cal.set(Integer.valueOf(year[i]).intValue(),Integer.valueOf(mon[i]).intValue(),Integer.valueOf(day[i]).intValue() , 0, 0, 0);
            setday[i] = cal.getTime();
        }
            for (int j=0;j<t;j++){
            caldroidFragment.setBackgroundResourceForDate(R.color.green,
                    setday[j]);
            caldroidFragment.setTextColorForDate(R.color.white, setday[j]);
            }
        }
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);

        if (caldroidFragment != null) {
            caldroidFragment.saveStatesToKey(outState, "CALDROID_SAVED_STATE");
        }

        if (dialogCaldroidFragment != null) {
            dialogCaldroidFragment.saveStatesToKey(outState,
                    "DIALOG_CALDROID_SAVED_STATE");
        }
    }

最佳答案

我重写了库的一部分 这导致了一个错误 因为我的jar不支持getChildFragmentManager() 我重写了getFragmentManager() 导致的错误

关于安卓重启日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21278285/

相关文章:

java - ImageURL 未填充在 Android 中 imageView 内的图像中

android - 为什么日期选择器会在我的 View 中添加日历?

Android - Calendar.getInstance 的小时值设置为零

java - 加载 fragment 时应用程序崩溃

android - 向 CalendarView 添加事件

java.lang.NullPointerException : Loading three charts (from github) into a 2x2 GridView

java - 在没有用户干预或 Intent 的情况下发送电子邮件

Android + 将 LoaderManager 与 ArrayAdapter 一起使用?

android - 如何使用 Android Studio 运行单元测试

java - DialogFragment 在 Android 4.4.2 中顶部有蓝线