【问题标题】:Calling Fragment's method from Activity doesn't work?从Activity调用Fragment的方法不起作用?
【发布时间】:2015-07-26 21:56:09
【问题描述】:

我关注this 问题并尝试调用我的片段中的方法。我正在尝试从活动中调用该方法。但是它没有识别片段的方法。这是我的代码:

XML:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/peoplefragment">

    <ListView
        android:id="@+id/searchpeople_list"
        android:layout_height="fill_parent"
        android:layout_width="match_parent"
        android:scrollbars="none"
        android:background="#fff">
    </ListView>

</RelativeLayout>

片段代码:

    public class SearchPeopleTab extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        {          
            View v = inflater.inflate(R.layout.activity_search_people_tab, container, false);
            View rootView = inflater.inflate(R.layout.activity_search_people_tab, container, false);
            return rootView;
        }

        public static void UpdateResults(String requestSearch)
        {
               new GetSearchResults(requestSearch).execute();
        }

class GetSearchResults extends AsyncTask<Void, Void, Void> {

        String requestSearch;

        GetSearchResults(String searchtext)
        {
            this.requestSearch = searchtext;
        }

        @Override
        protected Void doInBackground(Void... params) {
    }

活动代码:(调用 Fragment 的方法)

 private void PopulateResults() {

        FragmentManager manager = (FragmentManager) getSupportFragmentManager();
        Fragment fragment = manager.findFragmentById(R.id.peoplefragment);
        fragment.UpdateResults(requestSearch); //thats the method in the fragment. 

}

“UpdateResults()”部分带有下划线,以下消息作为错误给出:

无法解析方法 UpdateResults()

似乎找不到方法。我做错了什么?

【问题讨论】:

    标签: java android android-layout android-fragments android-fragmentactivity


    【解决方案1】:
    1. 从方法中删除关键字static

    2. 此外,将片段存储在您创建的 SearchPeopleTab 引用变量中。

      FragmentManager的存储其实不需要线,直接使用getSupportFragmentManager();即可

      //FragmentManager fm = (FragmentManager) getSupportFragmentManager();
      SearchPeopleTab fragment = (SearchPeopleTab) getSupportFragmentManager().findFragmentById(R.id.peoplefragment);
      fragment.UpdateResults();
      

    使用静态方法时,使用类名调用它们。 当您希望在特定对象上调用方法时,该方法不应是静态的。

    【讨论】:

    • 如果我也想向方法发送参数怎么办?如 fragment.UpdateResults("hey");
    • 是的,工作方式相同。 fragment.UpdateResults("hey"); 如果这是解决方案,请将答案标记为已接受。帮助我获得声望点。 :) 谢谢。
    • 是的,并且也要更改片段中方法的代码。
    【解决方案2】:

    您需要将 Fragment 转换为您定义的类

    private void PopulateResults() {
    
        FragmentManager manager = (FragmentManager) getSupportFragmentManager();
        SearchPeopleTab fragment = (SearchPeopleTab)manager.findFragmentById(R.id.peoplefragment);
        fragment.UpdateResults(); //thats the method in the fragment. 
    
    }
    

    【讨论】:

    • 如果我也想向方法发送参数怎么办?如 fragment.UpdateResults("hey");它返回一个空指针
    • 只要声明方法接受一个String参数。调用 UpdateResults() 时是否得到空指针?如果是这样,您能显示该方法的代码吗?
    • 更新了问题:) 我相信一定是因为我试图从静态方法调用 AsyncTask
    猜你喜欢
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    相关资源
    最近更新 更多