NullPointerException when calling a Fragment method from Activity?

Nikhil

I'm getting a NullPointerException when calling a Fragment's method.

In my MainActivity:

  AddTab fragObj = (AddTab) getSupportFragmentManager().findFragmentById(R.layout.fragment_add);
 fragObj.receiveURL(sharedText);

receiveURL() is a method in my Fragment AddTab.

I'm using pagerslidingtabstrip in my app to create tabs and that Fragment is created by it's PagerAdapter class.

I'm not sure if it is right to create a new instance of the Tab, because already one exists. I don't know how to get access to that instance as well.

I'm getting a NullPointerException now. Can anyone please help me on how to call that method in the fragment?

EDIT :

AddTab.java :

public class AddTab extends Fragment implements View.OnClickListener {

...

}

fragment_add.xml :

<FrameLayout 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" tools:context="com.example.nikhil.amazon1.Add">


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:text="@string/URL"
        android:hint="@string/hint"
        android:layout_marginTop="200dp"
        android:layout_gravity="center_horizontal|top" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Track"
        android:id="@+id/button"
        android:layout_gravity="center" />

</FrameLayout>
2Dee

If you use the <fragment> tag to include a Fragment in your layout, you can use find findFragmentById using the id you assigned for the Fragment in the xml. Example :

<fragment
    ...
    android:id="@+id/fragment_id/>

and

AddTab fragObj = (AddTab)getSupportFragmentManager().findFragmentById(R.id.fragment_id);

If you added your Fragment dynamically, use tags :

String TAG = "fragment tag";

FragmentTransaction ft;
ft.add(containerViewId, fragment, TAG);

and

AddTab fragObj = (AddTab)getSupportFragmentManager().findFragmentByTag(TAG);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

NullPointer when calling Fragment method from activity

From Dev

When Calling Other Activity Method Showing NullPointerException

From

Calling a Fragment method from a parent Activity

From Dev

calling parent activity method from fragment

From Dev

Calling fragment method from activity Android

From Dev

App Crashing when calling a Fragment from an Activity

From Java

Calling a function when an activity is returned to from fragment

From Dev

Calling Activity from Fragment

From Dev

Calling fragment from activity

From Dev

NullPointerException when calling a method from a different class

From Java

NullPointerException when calling method from another class

From Dev

NullPointerException when using fragment in Activity

From Dev

Calling Fragment method from Activity,findFragmentById returns null

From Dev

Calling Fragment's method from Activity doesn't work?

From Dev

Calling Activity method inside Fragment that is not attached to Activity

From Dev

NullPointerException on calling Activity from FragmentActivity

From Java

Starting Fragment activity from an activity causes NullPointerException

From Dev

Calling a custom fragment from an activity

From Dev

calling an activity from fragment activity through Intent

From Dev

Calling Fragment Method by Parent FragmentActivity, getting NullPointerException

From Dev

NullPointerException when calling a mocked method

From Dev

NullPointerException when calling mocked method

From Dev

NullPointerException when calling EJB method

From Dev

NullPointerException when calling Redis method

From Dev

calling fragment method in Activity not working kotlin

From Dev

NullPointerException when calling a method from a dynamic object array

From Dev

SpringBoot @Autowired NullPointerException when calling method from service class

From

Call an activity method from a fragment

From Dev

Fragment method not accessible from Activity

Related Related

  1. 1

    NullPointer when calling Fragment method from activity

  2. 2

    When Calling Other Activity Method Showing NullPointerException

  3. 3

    Calling a Fragment method from a parent Activity

  4. 4

    calling parent activity method from fragment

  5. 5

    Calling fragment method from activity Android

  6. 6

    App Crashing when calling a Fragment from an Activity

  7. 7

    Calling a function when an activity is returned to from fragment

  8. 8

    Calling Activity from Fragment

  9. 9

    Calling fragment from activity

  10. 10

    NullPointerException when calling a method from a different class

  11. 11

    NullPointerException when calling method from another class

  12. 12

    NullPointerException when using fragment in Activity

  13. 13

    Calling Fragment method from Activity,findFragmentById returns null

  14. 14

    Calling Fragment's method from Activity doesn't work?

  15. 15

    Calling Activity method inside Fragment that is not attached to Activity

  16. 16

    NullPointerException on calling Activity from FragmentActivity

  17. 17

    Starting Fragment activity from an activity causes NullPointerException

  18. 18

    Calling a custom fragment from an activity

  19. 19

    calling an activity from fragment activity through Intent

  20. 20

    Calling Fragment Method by Parent FragmentActivity, getting NullPointerException

  21. 21

    NullPointerException when calling a mocked method

  22. 22

    NullPointerException when calling mocked method

  23. 23

    NullPointerException when calling EJB method

  24. 24

    NullPointerException when calling Redis method

  25. 25

    calling fragment method in Activity not working kotlin

  26. 26

    NullPointerException when calling a method from a dynamic object array

  27. 27

    SpringBoot @Autowired NullPointerException when calling method from service class

  28. 28

    Call an activity method from a fragment

  29. 29

    Fragment method not accessible from Activity

HotTag

Archive