title: Android: Calling a method which is not in an Activity by clicking a Button
body: <p>I have 2 Java classes with 2 XML layout files in my Android Studio Project. The first class is my MainActivity class and container for the map which the second class displays. The layout works fine, but I have a Button in the XML File of my second class, which is set to call a method in my second class when clicked. But the button can only call methods from my MainActivity class. How can i let the Button call another method, let's say a method from another class than my MainActivity class?</p>
  
  <p>Your help is very much appreciated.</p>
  
  <p>Thanks in advance.</p>
  
  <p>/Edit:</p>
  
  <p>I can call the other method now, but everytime I try accessing something from the MainActivity class the app crashes. Code example: </p>
  
  <pre><code>public void test() { 
    TextView tv = (TextView) getActivity().findViewById(R.id.textView); 
    tv.setText("2"); 
  } 
  </code></pre>
  
  <p>This is the method from the second class which is called out of the MainActivity class. The call works fine, but I can't access anything from the layout. If it helps the class is a Fragment, which contains a MapView: </p>
  
  <pre><code>public class MapsFragment extends Fragment {
  </code></pre>