java - 日历代码在 Activity 中有效但在 fragment 中无效

标签 java android android-fragments calendar fragment

我在使用 Calendar 的 Activity 类中编写了代码,然后将其移至 Fragment,并通过调用以下方法将此 fragment 动态附加到 Activity:`

mFragmentManager = getFragmentManager();

        //mButtonsFragment = mFragmentManager.findFragmentById(R.id.fragment_buttons);
        mLayout1to6 = (FrameLayout) findViewById(R.id.fragment_1to6);
        final FragmentTransaction ft = mFragmentManager.beginTransaction();

        ft.add(R.id.fragment_1to6, f1to6);
        ft.addToBackStack(null);
        ft.commit();

        mFragmentManager
                .addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
                    public void onBackStackChanged() {
                        setLayout();
                    }
                });


        mFragmentManager.executePendingTransactions();
`

和 fragment 代码本身是这样的:

public class Fragment_1to6 extends Fragment {
    private Activity mActivity;

    Calendar calendar;
    String month_name;

    TextView month1;
    TextView month2;
    TextView month3;
    TextView month4;
    TextView month5;

    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fragment_1to6, container, false);
        month1 = (TextView) mActivity.findViewById(R.id.monthView1);
        month2 = (TextView) mActivity.findViewById(R.id.monthView2);
        month3 = (TextView) mActivity.findViewById(R.id.monthView3);
        month4 = (TextView) mActivity.findViewById(R.id.monthView5);

        calendar = Calendar.getInstance();
        setMonths();

            return view;

        }
    public void setCurrentMonthName() {

        SimpleDateFormat month_date = new SimpleDateFormat("MMM");
        month_name = month_date.format(calendar.getTime());
        month1.setText(month_name);
    }


    protected void getNextMonth(int i) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("MMM");

        switch (i) {
            case 1:
                calendar.add(Calendar.MONTH, 1);
                month2.setText(dateFormat.format(calendar.getTime()));
                break;
            case 2:
                calendar.add(Calendar.MONTH, 1);
                month3.setText(dateFormat.format(calendar.getTime()));
                break;
            case 3:
                calendar.add(Calendar.MONTH, 1);
                month4.setText(dateFormat.format(calendar.getTime()));
                break;
            case 4:
                calendar.add(Calendar.MONTH, 1);
                month5.setText(dateFormat.format(calendar.getTime()));
                break;
        }

    }
    public void setMonths() {
        setCurrentMonthName();
        getNextMonth(1);
        getNextMonth(2);
        getNextMonth(3);
        getNextMonth(4);
    }


}
`

但是,当我运行它时,它在我在 textViews 上设置文本的那一行给出 NullPointerException,说它不能在空对象引用上设置文本。

最佳答案

替换这段代码

month1 = (TextView) mActivity.findViewById(R.id.monthView1);
month2 = (TextView) mActivity.findViewById(R.id.monthView2);
month3 = (TextView) mActivity.findViewById(R.id.monthView3);
month4 = (TextView) mActivity.findViewById(R.id.monthView5);

month1 = (TextView) view.findViewById(R.id.monthView1);
month2 = (TextView) view.findViewById(R.id.monthView2);
month3 = (TextView) view.findViewById(R.id.monthView3);
month4 = (TextView) view.findViewById(R.id.monthView5);

Activity 是您在其上膨胀 fragment 的地方。但是所有 TextView 都是 fragment xml 的一部分,您必须从 fragment 的 View 而不是 Activity 的 View 访问它们。

关于java - 日历代码在 Activity 中有效但在 fragment 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31097609/

相关文章:

android - javaMail API 强制 SSL 加密以进行 SMTP 身份验证

android - 对 fragment 进行动画处理,使其部分脱离屏幕

java - qpid如何配置接收器

java - 如何在 Java AppEngine 上存储大 double 组

java - MySQL - 对象不为空?

java - 在java中合并字节

Android View 以编程方式查看 XML

android - 为什么 OrmLite/Sqlite/Android 中的表周围需要 "`?

java - 当用户单击后退时,有什么方法可以在 fragment 中调用 saveNote() 吗?

android - 如何避免将相同的 fragment 添加到堆栈