` this app i have two fragment ,  when app run in pot rate mode 2nd fragment is open in new activity when in tablet mood both fragment is stuck together but when i try to initialize the fragment A object in main activity the app is crash then i try to find error i find find Fragment By id is return null to fragment A  object 


find fragment by id is return null to my fragment object`

    main Activity `enter code here`
public class MainActivity extends AppCompatActivity implements  FragmentA.communicator  {

    FragmentA fa = new FragmentA();
    FragmentB fb;
    FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragmentManager = getFragmentManager();

          fa = (FragmentA) fragmentManager.findFragmentById(R.id.fragment);
        fa.setCommunicator(this);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void respond(int index) {

       fb= (FragmentB) fragmentManager.findFragmentById(R.id.fragment2);

        if(fb!=null && fb.isVisible())
        {
             fb.changeData(index);
        }
        else
        {
            Intent intent = new Intent(this ,AnotherActivity.class);
            intent.putExtra("index",index);
            startActivity(intent);
        }
    }
}

    enter code here
    main_activity.xml
    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="ameerhamza.assembly8086programmingprograms.FragmentA"
        android:id="@+id/fragment" />

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="ameerhamza.assembly8086programmingprograms.FragmentB"
        android:id="@+id/fragment2"
        android:layout_below="@+id/fragment"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout> enter code here
FragmentA `enter code here`
    enter code here 
public class FragmentA extends Fragment implements AdapterView.OnItemClickListener {
    private ListView listView1;
    String[] days = {"a","b","c"};
    communicator communicator;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_a, container, true);
        listView1 =(ListView) view.findViewById(R.id.listView);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(),R.array.chapter,android.R.layout.simple_list_item_1);
        // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
        listView1.setAdapter(adapter);
        listView1.setOnItemClickListener(this);
        return  view;
    }

    public  void setCommunicator(communicator c)
    {
        this.communicator =c;
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        communicator.respond(position);
    }

    public interface  communicator{

        public  void  respond(int index);
    }
}

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>`enter code here`

`


`

`