简体   繁体   English

如何从Main活动中调用片段的功能

[英]How to call the Function of the Fragment from the Main activity

First let me tell you what I am trying to do. 首先让我告诉您我要做什么。 I have an activity with navigation drawer and some fragment classes. 我有一个带有导航抽屉和一些片段类的活动。 On activity resume I am calling a MapFragment which I am using to show the user its current location with the blue dot on google map. 在活动履历表上,我正在调用MapFragment,它用于向用户显示其当前位置以及在Google地图上的蓝点。

This is running successfully . 这正在成功运行。

Now from the navigation drawer I want to show the pins on the map , so for this I am calling the function which is used to download pins from the server and to show them on map. 现在,我要从导航抽屉中在地图上显示图钉,为此,我正在调用用于从服务器下载图钉并在地图上显示图钉的函数。 This function is named as GetAllPins , I am doing this in the following manner using Ion library: 此函数名为GetAllPins,我使用离子库通过以下方式进行此操作:

 public void GetAllPins() {

        progressDialog.show();
 Ion.with(getActivity())
                .load(MyServiceUrl)
                .progressHandler(new ProgressCallback() {
                    @Override
                    public void onProgress(long downloaded, long total) {
                        if (downloaded == total) {
                            progressDialog.dismiss();
                        }
                    }
                })
                .setMultipartParameter("name", "Stacy")
                .setMultipartParameter("latitude","18.92239u")
                .setMultipartParameter("longitude", "24.98329")
                .asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {
                        if (result == null) {
                            progressDialog.dismiss();
                            Toast.makeText(getActivity(), "Error in establishing connection with server", Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(getActivity(), "Completed successfully", Toast.LENGTH_SHORT).show();
}
}
);
}

Note : This is working fine and plotting pins successfully when called inside MyMapFragment's onActivityCreated Method , but giving me a NullpointerException when I try to call it from the MainActivity. 注意:在MyMapFragment的onActivityCreated方法中调用此方法时,它工作正常并成功绘制了引脚,但是当我尝试从MainActivity调用它时,却给了我NullpointerException。

In MainActivity I am calling it in switch case as I am using SwitchCase to navigate to fragments accordingly . 在MainActivity中,我使用SwitchCase导航到相应的片段时,在切换情况下调用它。

public void navigateToFragment(int pos) { 公共无效navigationToFragment(int pos){

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    final MyMapFragment myMapFragment = new MyMapFragment();
    switch (pos) {


        case R.id.home:
            if(!isFirstRun) {

                fragmentTransaction.replace(R.id.frame, myMapFragment, "MapFragment").addToBackStack("MapFragment").commit();
            }else{
                isFirstRun=false;
            }
            //
            break;

        case R.id.mark_pin:
            isFirstRun=false;
            if (myMapFragment!=null){
           myMapFragment.GetAllPins();}
                        break;
                    }

So I am getting a Null pointerException on ProgressDialog , but if I comment out the following an error occures. 所以我在ProgressDialog上得到了一个N​​ullpointerException异常,但是如果我注释掉以下内容,则会发生错误。

 FATAL EXCEPTION: main
java.lang.NullPointerException: Can not pass null context in to retrieve ion instance

Tell me how to call this function from the main activity to show the pins on the map using that MyMapFragment. 告诉我如何从主活动中调用此函数,以使用该MyMapFragment在地图上显示图钉。 I do not want to make a different fragment of map to show the pins on it. 我不想制作不同的地图片段以显示地图上的图钉。 Please help me. 请帮我。

From the log itself, itz clear that the error iz where the context is called... From fragment, u will get proper context when you call getActivity()... But when u call the same method from MainActivity, the getActivity() will return null... So itz better to add context as a parameter to the function and when calling from fragment, call it by passing getActivity() and from MainActivity call it by passing 'this'... 从日志本身,itz清除了调用上下文的错误iz ...从片段中,当您调用getActivity()时,您将获得适当的上下文...但是当您从MainActivity中调用相同的方法时,getActivity()将返回null ...因此,最好将上下文作为参数添加到函数中,并且从片段调用时,通过传递getActivity()进行调用,从MainActivity传递通过'this'进行调用...

I didnt Test it... But it should work... 我没有测试...但是应该可以...

Call the function from MainActivity like this, fragment.GetAllPins(MainActivity.this); 像这样从MainActivity调用该函数fragment.GetAllPins(MainActivity.this);

When u call from fragment, it should be like this, GetAllPins(getActivity()); 当您从片段调用时,应该是这样, GetAllPins(getActivity());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM