title: Communication between Activity and Fragment
body: <p>Hello Everyone I'm developing an app,in order to explain my situation I've created simple illustrator. When i click the button inside another activity i want to send data to fragment. I use bundle its gives me nullpointerexception. I tried to use method inside another activity it gives me error again. How can i pass data from Activity to Fragment thanks in advance. </p>
  
  <p>Here my codes. </p>
  
  <p><strong>Edit1 :</strong> I think i explained wrong. Look at photo that i've uploaded. When i click the white button anotheractivity is opening. When i typing some text inside anotheractivity and click the button inside anotheractivity, then i want to send datas Fragment class. I hope i was able to explained. Thanks.</p>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Button btn = (Button) findViewById(R.id.button2);
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent i = new Intent(MainActivity.this,AnotherActivity.class);
                  startActivity(i);
  
              }
          });
      }
  }
  </code></pre>
  
  <p><strong>AnotherActivity</strong></p>
  
  <pre><code>public class AnotherActivity extends Activity {
      EditText et;
      Button btn;
      public String ets;
      FragmentClass myFragment = new FragmentClass();
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.anotheractivity);
          btn = (Button) findViewById(R.id.button);
          et= (EditText) findViewById(R.id.editText);
          ets = et.getText().toString();
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  myFragment.Test(ets);
  
              }
          });
      }
  }
  </code></pre>
  
  <p><strong>Fragment</strong></p>
  
  <pre><code>public class FragmentClass extends Fragment {
      TextView tv;
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.fragment,container,false);
          tv = (TextView)v.findViewById(R.id.textView);
  
          return v;
      }
  
  
      public void Test(String name){
         Toast.makeText(getActivity(),name,Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/8Q3F0.png" alt="enter image description here"></p>