title: Pass data from Activity to Fragment Class Cast Exception
body: <p>There are so many ways but i am using this one:</p>
  
  <p>Activity:(Main2Activity)</p>
  
  <pre><code>public class Main2Activity extends AppCompatActivity{
  
  private String myString = "hello";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_my);
      ...
  }
  
  public String getMyData() {
      return myString;
  }
  } 
  </code></pre>
  
  <p>Fragment:</p>
  
  <pre><code>public class MyFragment extends android.support.v4.app.Fragment{
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
      Main2Activity activity = (Main2Activity ) getActivity();
      String myDataFromActivity = activity.getMyData();
      return view;
  }
  }
  </code></pre>
  
  <p>Main Activity:</p>
  
  <pre><code>    public class MainActivity extends AppCompatActivity {
  
     -------
      @Override
      protected void onCreate(Bundle savedInstanceState) {
       }
        ---
       private void setupViewPager(ViewPager viewPager) {
  
      Adapter adapter = new Adapter(getSupportFragmentManager());
      adapter.addFragment(new MyFragment(), "MyFragment");
  
      viewPager.setAdapter(adapter);
      }
  </code></pre>
  
  <p>BUT its giving me class cast exception:</p>
  
  <pre><code>java.lang.ClassCastException: com.example.dev03.xyz.Activities.MainActivity cannot be cast to com.example.dev03.xyz.Fragments.MyActivity
  
  java.lang.ClassCastException: com.example.dev03.xyz.Fragments.MainActivity cannot be cast to com.example.dev03.xyz.Fragments.Main2Activity
  </code></pre>
  
  <p>-- Package is same.</p>
  
  <p>Thanks</p>