In portrait, it gives null point exceptioin when the list item is clicked but landscape works just fine... In AnotherAcitivity.java Fragment f2 gets null while debugged...

```
package com.example.myfragmentqadvancedel;

import com.example.myfragmentqadvancedel.FragmentA.Communicator;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;

public class MainActivity extends Activity implements Communicator{
FragmentA f1;
FragmentB f2;
FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    manager = getFragmentManager();
    f1=(FragmentA) manager.findFragmentById(R.id.fragment1);
    f1.setCommunicator(this);
}

@Override
public void respond(int index) {
    f2=(FragmentB) manager.findFragmentById(R.id.fragment2);
    if (f2!=null && f2.isVisible()) {
        f2.changeData(index );
    }else {
        Intent intent = new Intent(this,AnotherActivity.class);
        intent.putExtra("index", index);
        startActivity(intent);
    }
}
}

```

**FragmentA**

```
package com.example.myfragmentqadvancedel;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FragmentA extends Fragment implements OnItemClickListener  {
ListView list;
Communicator communicator;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.fragment_a, container,false);

    return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    list=(ListView) getActivity().findViewById(R.id.listView1);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.chapters, android.R.layout.simple_list_item_1);
    list.setAdapter(adapter);
    list.setOnItemClickListener(this);
}

public void setCommunicator(Communicator communicator) {
    this.communicator = communicator;
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int i, long arg3) {
    communicator.respond(i);

}

public interface Communicator{
    public void respond(int index);
   }
}

```

**AnotherActivity.java**

```
package com.example.myfragmentqadvancedel;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

public class AnotherActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_another);

Intent intent = getIntent();
int index=intent.getIntExtra("index", 0);
FragmentB f2= (FragmentB) getFragmentManager().findFragmentById(R.id.fragment2);
if(f2!=null)   <-- f2 gets null, index gets +ve value as checked in debugger
f2.changeData(index);
  }
}

```

**FragmentB**

```
package com.example.myfragmentqadvancedel;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentB extends Fragment{
TextView text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_b, container,false);

    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    text=(TextView) getActivity().findViewById(R.id.textView1);
}

public void changeData(int index){
    String[] descriptions = getResources().getStringArray(R.array.description);
    text.setText(descriptions[index]);
  }
}

```

**logcat**
 05-27 16:40:37.866: E/AndroidRuntime(961): FATAL EXCEPTION: main

```
05-27 16:40:37.866: E/AndroidRuntime(961): java.lang.NullPointerException 

05-27 16:40:37.866: E/AndroidRuntime(961):  at     com.example.myfragmentqadvance.MainActivity.respond(MainActivity.java:26)

05-27 16:40:37.866: E/AndroidRuntime(961):  at     com.example.myfragmentqadvance.Fragment1.onItemClick(Fragment1.java:42)

05-27 16:40:37.866: E/AndroidRuntime(961):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)

05-27 16:40:37.866: E/AndroidRuntime(961):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)

```

**activity\_main.xml**

```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<fragment
    android:id="@+id/fragment1"
    android:name="com.example.myfragmentqadvancedel.FragmentA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

```

**fragment\_a.xml**

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="#0f0">

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>
</LinearLayout>

```

**fragment\_b.xml**

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00f" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" 
    android:textColor="#fff"/>
</LinearLayout>

```

**activity\_another.xml**

```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AnotherActivity" >

<fragment
    android:id="@+id/fragment2"
    android:name="com.example.myfragmentqadvancedel.FragmentB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="38dp" />
</RelativeLayout>

```