DialogFragment意外行为 [英] DialogFragment behaving unexpectedly

查看:150
本文介绍了DialogFragment意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我已经使用这个DialogFragment在我的应用程序来显示日期选择器

Hi I have used this DialogFragment to display date picker in my app

public class DateDialogFragment extends DialogFragment  implements DatePickerDialog.OnDateSetListener{

        public DateDialogFragment()
        {
        }
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            Calendar cal=Calendar.getInstance();
            int year=cal.get(Calendar.YEAR);
            int month=cal.get(Calendar.MONTH);
            int day=cal.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            showSetDate(year,monthOfYear,dayOfMonth);
        }

        }

    public void showSetDate(int year,int month,int day) {
    text.setText(year+"/+"+month+"/"+day);
    }

如果在我的previous应用程序使用了相同的。
最近,DateDialogFragment strangly行为。在Eclipse中是显示错误DateDialogFragment应static.But当我清理项目一次。它并不显示在项目中的任何错误,它运行完美。我曾经使用过此 DialogFragment 消失了,它证实,它需要类是静态的。但为什么它让我用这个eventhough当我清理项目,我没有给静态的。这种情况最近previously它并没有显示出任何这样的错误。同样没有表现出任何错误,当我和我的团队成员共享项目。为什么表现这样的..

If have used the same in my previous apps. Recently it DateDialogFragment behaving strangly. In Eclipse it is showing the error DateDialogFragment should be static.But When I clean the project once. It doesn't showing any error in the project and it runs perfectly. I have gone through this DialogFragment and it confirms that it needs class to be static. But why is it allowing me use this eventhough I haven't give static when I clean the project. This happens recently previously it hasn't show any error like this. The same is not showing any error when I share the project with my team members. Why is it behaving like this..

推荐答案

您看到这种情况的原因是因为Android的有时需要实例化片段自身。这适用于的任何片段

The reason you are seeing this is because Android sometimes needs to instantiate that Fragment on its own. This applies to any Fragment.

当你创建一个静态内部类,这意味着它不依赖于外部类的任何特定实例。因此,让我们说你有:

When you create a static inner class, that means it is not tied to any specific instance of the outer class. So let's say you have:

public class A {
  public static class B {
    // ...
  }
  public class C {
    // ...
  }
}

在这种情况下,你不能做新C()从外面 A ,因为所有实例 C 属于一个 A 对象。您的可以的,但是,确实新B()新AB()

In this case, you cannot do new C() from outside A because all instances of C belong to an A object. You can, however, do new B() or new A.B().

这同样适用于所述片段; Android的不能做新DateDialogFragment()如果该类不是一成不变的。你没有得到一个错误的原因(尽管皮棉的的告诉你什么),是因为你实例化 DateDialogFragment 自己。

The same applies to the fragment; Android cannot do new DateDialogFragment() if the class is not static. The reason you are not getting an error (though Lint should be telling you) is because you are instantiating the DateDialogFragment yourself.

不过,如果你触发类似改变方向时,不重新创建片段手动,Android将会为你做它。由于无法这样做,它会崩溃。

However, if you trigger something like an orientation change and don't recreate the Fragment manually, Android will do it for you. Being unable to do so, it will crash.

如果这个类的的静态,不过,Android的可以的创建它的一个实例。因此,嵌套片段类应的总是的是静态

If the class is static, however, Android can create an instance of it. Therefore, a nested Fragment class should always be static.

这篇关于DialogFragment意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆