Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从android中的主活动调用Fragment函数但不起作用_Android_Function_Android Fragments_Android Activity - Fatal编程技术网

从android中的主活动调用Fragment函数但不起作用

从android中的主活动调用Fragment函数但不起作用,android,function,android-fragments,android-activity,Android,Function,Android Fragments,Android Activity,实际上,我想从主活动调用特定ClickListner上的片段函数,但不起作用。 以下是我的片段xml代码: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

实际上,我想从主活动调用特定ClickListner上的片段函数,但不起作用。 以下是我的片段xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipelayoutm"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/mThirdRecyclerview"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            class="android.support.v7.widget.RecyclerView"/>
    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"
        >
        <ExpandableListView
            android:id="@+id/lvExp"
            android:layout_marginTop="390dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:divider="@color/colorPrimary"
            android:layout_width="match_parent"/>

    </android.support.design.widget.NavigationView>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true"
        app:itemTextColor="@color/black">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include layout="@layout/practicefilterdesign" />
    </LinearLayout>
    </android.support.design.widget.NavigationView>
    <!--app:menu="@menu/activity_main_drawer"-->
</android.support.v4.widget.DrawerLayout>
以下是我的片段函数:

 public void SearchCategoresFunction(final String searchTxt){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dataStoredArrayList.clear();
                myRecyclerAdapter.notifyDataSetChanged();
              SecondParseDataClass  mparser = new SecondParseDataClass(getContext());
                mparser.setOnDataRetrievalCallback(new OnDataRetrievalCallback() {
                    @Override
                    public void onDataRetrieval(ArrayList<DataStored> dataSet) {
                        dataStoredArrayList.addAll(dataSet);
                        myRecyclerAdapter.notifyDataSetChanged();
                    }
                });
                mparser.execute("http://192.168.3.10/datafetchwithsubcatego.php", "3",searchTxt);

            }
        }, 0);
    }
public void SearchCategoresFunction(最终字符串searchTxt){
new Handler().postDelayed(new Runnable()){
@凌驾
公开募捐{
dataStoredArrayList.clear();
myRecyclerAdapter.notifyDataSetChanged();
SecondParseDataClass mparser=新的SecondParseDataClass(getContext());
setOnDataRetrievalCallback(新的OnDataRetrievalCallback(){
@凌驾
公共数据检索(ArrayList数据集){
dataStoredArrayList.addAll(数据集);
myRecyclerAdapter.notifyDataSetChanged();
}
});
mparser.execute(“http://192.168.3.10/datafetchwithsubcatego.php“,”3“,搜索文本);
}
}, 0);
}
以下是我的主要活动xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipelayoutm"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/mThirdRecyclerview"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            class="android.support.v7.widget.RecyclerView"/>
    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"
        >
        <ExpandableListView
            android:id="@+id/lvExp"
            android:layout_marginTop="390dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:divider="@color/colorPrimary"
            android:layout_width="match_parent"/>

    </android.support.design.widget.NavigationView>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true"
        app:itemTextColor="@color/black">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include layout="@layout/practicefilterdesign" />
    </LinearLayout>
    </android.support.design.widget.NavigationView>
    <!--app:menu="@menu/activity_main_drawer"-->
</android.support.v4.widget.DrawerLayout>


请指导我如何删除此错误

只需创建一个类来保存数据

public static class ClickEvent { /* Additional fields if needed */ }
在你的碎片上添加

private onClickFromActivity(){ 
//write click operation here
}

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(ClickEvent event) {
   onClickFromActivity()
};

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
}
当您想要触发片段方法时,请将其写在活动代码上

EventBus.getDefault().post(new ClickEvent());

这是在android中进行交互的最佳方式

尝试使用getSupportFragmentManager()而不是getFragmentManager(),同时发布您的活动xml,它会在findFragmentById(R.id.swipelayoutm)上给我错误信息;发布您的活动,请检查我是否已编辑代码并尝试此操作。您基本上是在创建一个与片段通信的接口。