【问题标题】:Call a non static method in fragment from activity?从活动中调用片段中的非静态方法?
【发布时间】:2015-07-06 08:19:47
【问题描述】:

我有一个通过 ViewPagerAdapter 管理四个片段的活动。 从我的活动中,我想调用一个方法:

public void openButtons(){
//mPosition is a position of pager

    if (mPosition==0){
        Fragment1 fragment = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment1);
        fragment.openButtons();


    }
    if (mPosition==1){
        Fragment2 fragment = (Fragment2) getSupportFragmentManager().findFragmentById(R.id.fragment2);
        fragment.openButtons();
    }
    if (mPosition==2){
        ....
    }
    if (mPosition==3){
        ...
    }

}

如果我的片段中的方法被定义为非静态:

public void openButtons(){//some stuff}

我得到一个用于 fragment.openButtons () 行的空指针,无论位置和片段是什么。

如果方法声明为静态,就可以了。

public static void openButtons(){//some stuff}

方法的内容没有问题,因为问题和空方法一样。

所以我的问题是为什么我们必须在片段中定义静态方法?

'在这些情况下的原因:

public void openButtons(){
//mPosition is a position of pager

    if (mPosition==0){
        Fragment1.openButtons()


    }
    if (mPosition==1){
         Fragment2.openButtons()
    }
    if (mPosition==2){
        ....
    }
    if (mPosition==3){
        ...
    }

}

功能相同!

谢谢。

【问题讨论】:

  • 你为什么不做一个特定片段的静态对象引用?并且使用该 obj 您可以调用片段中的任何方法。无需定义静态方法。
  • 请参阅this link 可能会对您有所帮助。
  • getSupportFragmentManager().findFragmentById(R.id.fragment1); 返回 null 吗?
  • 从 FragmentManager 获取片段对象,然后调用非静态方法。
  • 也许投射可以帮助你:((Fragment1) fragment).openButtons();这适用于我的情况

标签: java android android-fragments android-viewpager static-methods


【解决方案1】:

将 null 转换为引用不会引发异常,但会引发异常。

使用 findFragmentById() 或 findFragmentByTag() 获取引用并检查其是否为空,如果不是,请检查引用的 isAdded() 或 isVisible()。

PlayerFragment p = (PlayerFragment) mManager.findFragmentById(R.id.bottom_container);
if( p != null){
  if(p.isAdded()){
    p.onNotificationListener.updateUI();
  }
}

【讨论】:

  • 我不明白您对链接的解释,该方法应称为静态方法??
  • 方法不一定是静态的。你必须调用片段方法所以你必须检查片段是否为空并且片段被添加并激活之后你必须调用片段的方法。所以在上面代码我必须检查所有这些条件
  • 这正是我不明白的:NullPointer execption 是针对方法的,以防它不是静态的被视为 null!那么为什么我没有空指针:Fragment1 fragment = (Fragment1) getSupportFragmentManager () findFragmentById (R.id.fragment1)。当方法是静态的,反之,当方法不是静态的!同样在您的代码中,函数在哪里:onNotificationListener?非常感谢您的帮助...
  • 在我的情况下,片段必须是活动的,因为由 viewPager 管理。当我从父活动调用方法时,片段可见
【解决方案2】:

因此, 在 viewPager 的情况下,通过 id 或 tag 找到片段的实例,不是正确的方法。

最好做到以下几点:

public void openButtons() {
    // mPosition is a position of pager

    ViewPagerAdapter adapter = ((ViewPagerAdapter) mViewPager.getAdapter());

    if (mPosition == 0) {
        Fragment fragment = adapter.getItem(0);
        ((Fragment1)fragment).openButtons();
    }

    if (mPosition == 1){
        Fragment fragment = adapter.getItem(1);
        ((Fragment2)fragment).openButtons();
    }

    if (mPosition == 2){
        ....
    }

    if (mPosition == 3){
        ...
    }
}

谢谢。

【讨论】:

    【解决方案3】:
    //From fragment to activty:
    ((YourActivityClassName)getActivity()).yourPublicMethod();
    
    
    
    //From activity to fragment:
    FragmentManager fm = getSupportFragmentManager();
    
    //if you added fragment via layout xml
    YourFragmentClass fragment = 
    (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
    fragment.yourPublicMethod();
    
    //If you added fragment via code and used a **tag** string when you added your 
    fragment, use **findFragmentByTag** instead:
    
    YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");
    

    【讨论】:

      【解决方案4】:

      您必须获取片段的实例,然后调用您的公共方法:

      在您的片段中:

       private static yourFragment instance;
      

      然后在您的片段的onCreateView 中:

        @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) {
      
              instance= this;
      
              View v = inflater.inflate(R.layout.fragment_tools, container, false);
              binding = FragmentToolsBinding.inflate(inflater, container, false);
      
              return v;
          }
      

      而且在你的片段中,你必须有一个返回实例的静态方法:

      public static yourFragment GetInstance()
      {
          return instance;
      }
      

      那么你的片段中有一个公共方法,你想像这样调用它:

      public  void  openButtons()
      {
          Toast.makeText(getActivity(), "Test", Toast.LENGTH_SHORT).show();
      }
      

      然后您可以获取片段实例并像这样调用您的非静态公共方法:

      public void openButtons() {
          // mPosition is a position of pager
      
          ViewPagerAdapter adapter = ((ViewPagerAdapter) mViewPager.getAdapter());
      
          if (mPosition == 0) {
              yourFragment frag = yourFragment.GetInstance();
                  frag.openButtons();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-21
        • 1970-01-01
        相关资源
        最近更新 更多