1

I'm trying to call a function of a given fragment from the outside: both from the main activity and from another fragment of the same level. In both cases the fragment retrieved with findFragmentById is null. I also tried with findFragmentByTag but with the same result.

CALL FOR TARGET FRAGMENT FUNCTION

FragmentManager fm = getFragmentManager();//if added by xml
MainFragmentLDash2 myFragment = (MainFragmentLDash2) getFragmentManager().findFragmentById(R.id.fd2);
if(myFragment == null){
    Toast.makeText(getContext(), "null", Toast.LENGTH_LONG).show();
} else {
    Toast.makeText(getContext(), "ok", Toast.LENGTH_LONG).show();
    myFragment.mediaDraw();
}

FUNCTION IN THE TARGET FRAGMENT I WANT TO CALL

public void mediaDraw() {
    Toast.makeText(getContext(), "drawFRAGMENT2", Toast.LENGTH_SHORT).show();
}

FRAME'S XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fd2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:background="@color/transparent"
    android:descendantFocusability="beforeDescendants"
    android:fillAfter="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:fromYScale="0.0"
    android:gravity="center_vertical"
    android:tag="firstFragmentWrapperDash2">

</FrameLayout>

LOGCAT

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
  at android.widget.Toast.<init>(Toast.java:109)
  at android.widget.Toast.makeText(Toast.java:268)
  at design.ubris.myuni.MainFragmentLDash2.mediaDraw(MainFragmentLDash2.java:101)
  at design.ubris.myuni.MainFragmentLSegr1.OnAsyncTaskComplete(MainFragmentLSegr1.java:171)
  at design.ubris.myuni.URLDataReader.onPostExecute(URLDataReader.java:112)
  at design.ubris.myuni.URLDataReader.onPostExecute(URLDataReader.java:26)
  at android.os.AsyncTask.finish(AsyncTask.java:692)
  at android.os.AsyncTask.-wrap1(AsyncTask.java)
  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:709)
  at android.os.Handler.dispatchMessage(Handler.java:105)
  at android.os.Looper.loop(Looper.java:156)
  at android.app.ActivityThread.main(ActivityThread.java:6523)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
sourav.bh
  • 467
  • 1
  • 7
  • 20

3 Answers3

0

It happens because you have not yet added the fragment to the fragment stack.

Ensure you do this:

MainFragmentLDash2 myFragment = new MainFragmentLDash2();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.fd2, myFragment).commit();

now you can call the fragment method

myFragment.mediaDraw();

Diego Alvis
  • 1,785
  • 1
  • 13
  • 14
0

Alternatively you can use findFragmentByTag:

getSupportFragmentManager().findFragmentByTag( "PREFS_FRAGMENT" );

Just make sure you pass the tag when you create the fragment:

getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container, new YourFragment(), "PREFS_FRAGMENT")
                    .commit();
RonTLV
  • 2,376
  • 2
  • 24
  • 38
0

From the log you attached, it seems you are receiving that NullPointerExeception for the context you passed in Toast creation.

Toast.makeText(getContext().....

You need to replace this getContext() by getActivity() like this:

Toast.makeText(getActivity()....

sourav.bh
  • 467
  • 1
  • 7
  • 20
  • I tried with getActivity (), getActivity (). getApplicationContext (), getContext (). getApplicationContext (), but it does not work. – Giorgio Cafiso Jan 09 '19 at 06:36
  • same crash as before? – sourav.bh Jan 09 '19 at 06:37
  • it enter in a loop: it come back to the previous activity, then enter in the in the target one, and so on. – Giorgio Cafiso Jan 09 '19 at 06:39
  • yes an infinite loop. Now it recognize the fragment that is not more null, but it has this beaviour when i call target fragment function. – Giorgio Cafiso Jan 09 '19 at 06:44
  • the code snippets you attached that show only the reason behind crash and my answer was to fix that `NullPointerException` crash, which is correct. Now you are experiencing a new issue, which might be caused by another part of your code flow. I would suggest to post a separate question with other relevant codes to describe the new problem, since you already got the answer of your original problem. And please consider of marking my answer as accepted. – sourav.bh Jan 09 '19 at 06:55
  • hi @Giorgio, if you kindly consider marking this answer accepted which resolved your crash in the question, other peoples facing similar trouble may get help from it. – sourav.bh Jan 10 '19 at 09:44