滑动标签与日历在片段中

时间:2014-12-16 10:24:17

标签: android xml android-fragments calendar

我正在使用两个滑动标签。现在我想在其中一个标签中使用Calendar,但它给了我错误。

错误

12-16 09:58:54.437: E/AndroidRuntime(2495): FATAL EXCEPTION: main
12-16 09:58:54.437: E/AndroidRuntime(2495): Process: com.project.homepagedemo, PID: 2495
12-16 09:58:54.437: E/AndroidRuntime(2495): java.lang.NullPointerException
12-16 09:58:54.437: E/AndroidRuntime(2495):     at com.bharatwellness.mainactivities.DiaryTabOneCalorieConsumed.onCreateView(DiaryTabOneCalorieConsumed.java:66)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.View.measure(View.java:16497)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.View.measure(View.java:16497)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.View.measure(View.java:16497)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.View.measure(View.java:16497)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.Choreographer.doFrame(Choreographer.java:544)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.os.Handler.handleCallback(Handler.java:733)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.os.Looper.loop(Looper.java:136)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at android.app.ActivityThread.main(ActivityThread.java:5001)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at java.lang.reflect.Method.invokeNative(Native Method)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at java.lang.reflect.Method.invoke(Method.java:515)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-16 09:58:54.437: E/AndroidRuntime(2495):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

我的oncreate方法

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

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

        _calendar = Calendar.getInstance(Locale.getDefault());
        month = _calendar.get(Calendar.MONTH) + 1;
        year = _calendar.get(Calendar.YEAR);
        Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " + "Year: "
                + year);

        selectedDayMonthYearButton = (Button) this.getView().findViewById(R.id.selectedDayMonthYear);
        selectedDayMonthYearButton.setText("Selected: ");

        prevMonth = (ImageView) this.getView().findViewById(R.id.prevMonth);
        prevMonth.setOnClickListener(this);

        currentMonth = (TextView) this.getView().findViewById(R.id.currentMonth);
        currentMonth.setText(DateFormat.format(dateTemplate,
                _calendar.getTime()));

        nextMonth = (ImageView) this.getView().findViewById(R.id.nextMonth);
        nextMonth.setOnClickListener(this);

        calendarView = (GridView) this.getView().findViewById(R.id.calendar);

        // Initialised
        adapter = new GridCellAdapter(getActivity().getApplicationContext(),
                R.id.calendar_day_gridcell, month, year);
        adapter.notifyDataSetChanged();
        calendarView.setAdapter(adapter);
        return rootView;
    }

我检查了两次但没有任何内容应该为空。正如我在主要活动中直接使用它一样有效。

1 个答案:

答案 0 :(得分:3)

更改

  selectedDayMonthYearButton = (Button) this.getView().findViewById(R.id.selectedDayMonthYear);

  selectedDayMonthYearButton = (Button) rootView.findViewById(R.id.selectedDayMonthYear);

并为所有其他Views执行相同操作。