Use a class in another class

Asked

Viewed 244 times

3

good morning.

I’m new to Java, but come on.

I have a java class and need to use all of its methods in another main class.

There’s a problem, I’m already using a extends in both classes.

I need to use all methods of this class(I put in txt for being great):

http://t4web.com.br/MainActivity

In that class:

public class SocialFragment extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  {
    return inflater.inflate(R.layout.social_layout,null);


}

}

Is there any way to perform this procedure without calling method by method ?

@Edit

My Mainactivity is my map with all business rules, I need to use it in one of them below.

inserir a descrição da imagem aqui

But I’m in trouble because I’m already using the extends in the 2 classes.

  • 1

    Show what you want to do, you may have it, it depends on what you want. Although I think not without being a monumental gambiarra.

  • @bigown, I did the editing. Thank you.

  • @tharleycarvalho you don’t need to flag your question, leave comments for all users to see. Signaling serves to indicate serious problems in a topic.

1 answer

2


In SocialFragment do:

MainActivity mainActivity = null;

@Override
public void onResume() {
     super.onResume();
     mainActivity = (MainActivity)getActivity();
}

@Override
public void onPause() {
    super.onPause();
    mainActivity = null;
}

Declare the methods you want to call as public in MainActivity and call them on SocialFragment thus:

if (mainActivity != null) {
    mainActivity.meuMetodo();
}
  • Any other suggestion without calling methods by methods?

  • If I understand correctly, you have a viewpager where each tab (screen) is a fragment, and you want to include the map in one of the screens. To do this you must create a new fragment and play all business rules related to the map in this fragment, removing them from Mainactivity.

  • When I try to call the methods he from the error in onResume, in fact he doesn’t even call the methods. Look at the error of Logcat > http://t4web.com.br/logs

  • Forget this code that I posted. Follow the guidance that I put on afterwards.

  • This code would only work if Socialfragment was added to Mainactivity but by Logcat it was added to an Activity called Start.

  • this is the problem, some functionalities does not work with Fragement, only with Fragmentactivity. In a class I am returning all Fragment: public Fragment getItem(int position) { switch (position){ case 0 : Return new Primaryfragment(); case 1 : Return new Socialfragment(); case 2 : Return new Updatesfragment(); } Return null; } But I can’t call it Fragmentactivity, the problem is just turning my Activity into a Framento.

  • I was able to convert my Activity to Fragment, however I am not able to create Supportmapfragment from google maps using Fragment, it follows the logcast error: http://t4web.com.br/logs . In the same file, just below is my Activity.

  • First, don’t call your Activity Mainactivity anymore because it is now a Fragment. Fragments are added to activities, so they are different concepts. Second, it is not consistent with the error it is giving, because the error occurs on line 73 of the setUpMapIfNeeded method and this method does not exist in Activity. Post the class consistent with the error you are giving.

  • thanks for your help. I was able to convert all the code to Fragment, about the previous error I was able to create Supportmapfragment using Mapview -> https://developers.google.com/android/reference/com/google/android/maps/MapView

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.