将参数设置为fragment会导致IllegalStateException

时间:2013-12-19 09:53:25

标签: android android-fragments nullpointerexception illegalstateexception

我使用以下代码为从XML扩充的片段设置参数:

  fragment_phone=(AddNewFragment)fm.findFragmentById(R.I'd.phone_fragment);
   Bundle args=fragment_phone.getArguments();
    if(args==null)
    {
        args=new Bundle();
        args.putString("hint","Phone");
        fragment_phone.set arguments(args);
      }
      else
            args.putString("hint","Phone");

   //Similarly for two other fragments that are also instances of AddNewFragment

我使用三个Bundle个对象,每个片段一个。 Logcat说Fragment is already active java.lang.IllegalStateException at android.support.v4.app.setArguments

我尝试删除导致setArguments的{​​{1}},当我打电话给他时:

NullPointerException

1 个答案:

答案 0 :(得分:1)

使用Fragments,您始终必须实现interface

  1. 在片段中创建与其他片段进行通信的interface

  2. interfaceMainActivity课程中实施Activity

  3. 致电getArguments中的onActivityCreated(),以避免任何NullPointerExceptions

  4. 有关详情,请参阅this