简体   繁体   中英

Calling object of a class from another

The below flowchart represents my classes. There are three classes,

  • Main Class
  • Left Adapter
  • Right adapter

the main class contains a horizontal scroll view with two fragments as its children (left & right adapter's)

the left adapter has an interface to connect with the right adapter.

the function/method of the interface resides in the main class

inside the left adapter both the right adapter and the interface is being called.

MY question is how to call the right adapter from the left adapter cos i keep getting null pointer exception

what i have done is initialized the right adapter in main class stored it in a static variable and called the static variable from my left adapter.

If I am not mistaken, what you are trying to achieve here is called the "Communicator" pattern, the idea is that, you have 2 fragments, you want to establish some type of communication between the two fragments, but if you directly call one fragment from another would make them to dependent and closely related to each other. So, you would create an public inner interface in one of the fragments, then have the "Main Class" implement that interface, then inside your implemented method, call the other fragment, this way, the main class acts as an relay between the two fragments, and what important here is that the 2 fragments are not aware or do not care about the existence of the other fragment. As long as, you implement a "middle man" to relay between the two fragments, then every thing should be good. So in one word, have your main class implement the interface you created in the fragment, then call the other fragment inside the implemented method.

Updated:

Override the onAttach() method, then try to use the passed in the activity instance and cast it to the interface, then call the implemented method from your fragment like this

private InnerInterface innerInterface;

public void onAttach(Activity activity) 
{
    try
    {
        innerInterface = (InnerInterface)activity;
    }
    catch (ClassCastException e)
    {
        // thrown if this activity did not implement the InnerInterface
    }
}

Then somewhere in your fragment that created the inner interface, call the implemented method by the Main Class like this innerInterface.implemetedMethod() . I hope this is understandable to you.

in your left adapter, create a null object of Interface (create a method in it to return right adapter getRightAdapter()) and assign this interface object when you create an object (in constructor or in new method) from main class(implements interface). now in main class, implement that method to return the object of right adapter (main class must have the object of right adapter). now wherever you want the right adapter to be accessed from left adapter, just call this interfaceObject.getRightAdapter();

this can also be done vice-versa

hope this will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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