0

i'm working with fragments. I have created a List fragment and a Detail fragment. when i click on an item in the list the app crashes and it gives the following null pointer exception.

Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

I tried solutions of similar question but nothing works for me.

here is my Main Activity:

public class MainActivity extends AppCompatActivity implements ListFrag.ItemSelected {

    TextView tvDescription;
    ArrayList<String> descriptions;


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

       tvDescription = (TextView) findViewById(R.id.tvDescription);

        descriptions = new ArrayList<>();
        descriptions.add("Description for item 1");
        descriptions.add("Description for item 2");
        descriptions.add("Description for item 3");


    }

    @Override
    public void onItemSelected(int index) {

        tvDescription.setText(descriptions.get(index));
    }
}

List Fragment:

public class ListFrag extends ListFragment {

    ItemSelected activity;

    public  interface ItemSelected
    {
        void onItemSelected(int index);
    }


    public ListFrag() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        activity = (ItemSelected) context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        ArrayList<String> data = new ArrayList<>();
        data.add("1.This is item 1");
        data.add("2.This is item 2");
        data.add("3.This is item 3");

        setListAdapter(new ArrayAdapter<>( getActivity() , android.R.layout.simple_list_item_1,data));
    }

    @Override
    public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {

        activity.onItemSelected(position);
    } }

Detail Fragment:

public class DetailFrag extends Fragment {



    public DetailFrag() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_detail, container, false);
    }
}

activity_main layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">


    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/listFrag"
        android:name="com.example.fragmentspart1.ListFrag"
        android:layout_width="8dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@layout/fragment_list" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/detailFrag"
        android:name="com.example.fragmentspart1.DetailFrag"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="2"
        tools:layout="@layout/fragment_detail" />


</LinearLayout>

fragment_list layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:background="#FFFFFF"
    tools:context=".ListFrag">


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

fragment_detail layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:background="@color/design_default_color_secondary"
    tools:context=".DetailFrag">


    <TextView
        android:id="@+id/tvDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:text="@string/textview"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>
Usama Ali
  • 1
  • 2

1 Answers1

0
(TextView) findViewById(R.id.tvDescription);

returns null in MainActivity because your TextView tvDescription is in fragment_detail layout.

You must move this line to onViewCreated() of your DetailFragment and in onItemSelected() you must create a new instance of DetailFragmet and pass data as arguments to the DetailFragment instance.

After you start the DetailFragment you can do following in DetailFragment:

    override onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
       tvDescription = (TextView) findViewById(R.id.tvDescription);
       // get your list item data from arguments
       tvDescription.setText(descriptions.get(index));
    }
memres
  • 439
  • 2
  • 9