Home > Enterprise >  TextView in Fragment Returning Null in Android Studio
TextView in Fragment Returning Null in Android Studio

Time:10-05

i am new to Android Studio.. i am facing null pointer exception error on my TextView inside a Fragment, please help..here is my code. this is nothing but a simple interface between two fragments. i am getting Null pointer exception error when i click on an item in ListView Fragment. for sure the TextView in DetailsFragment didn't assign or point to the source of whatever it is. please help on solving this.

   public class MainActivity extends AppCompatActivity implements list_Fragment.ItemSelected {

    ArrayList<String> Descriptions = new ArrayList<String>();;
    TextView tvDescription;

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

        Descriptions.add("Description for Item 1");
        Descriptions.add("Description for Item 2");
        Descriptions.add("Description for Item 3");
        Descriptions.add("Description for Item 4");
        tvDescription = findViewById(R.id.tvDescription);
    }

    @Override
    public void onItemSelected(int index) {
        tvDescription.setText(Descriptions.get(index));
    }}

and list_fragment code is

public class list_Fragment extends ListFragment {

    ArrayList<String> Data = new ArrayList<String>();


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

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

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity=(ItemSelected) context;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Data.add("Item 1");
        Data.add("Item 2");
        Data.add("Item 3");
        Data.add("Item 4");
        setListAdapter(new ArrayAdapter<String>(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);
    }
}

and the details Fragment code, where i am getting the error i believe, is

public class DetailsFragment extends Fragment {

    public DetailsFragment() {
        // Required empty public constructor
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

Main XML file

<?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:id="@ id/ll_Horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">


    <androidx.fragment.app.FragmentContainerView
        android:id="@ id/listfragmentView"
        android:name="com.example.fragmentcheck.list_Fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="@android:color/holo_blue_dark"
        tools:layout="@layout/fragment_list_" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@ id/detailsfragmentView"
        android:name="com.example.fragmentcheck.DetailsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/purple_200"
        tools:layout="@layout/fragment_details" />
</LinearLayout>

ListFragment XML file

<?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:id="@ id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/design_default_color_primary_variant"
    android:orientation="vertical"
    tools:context=".list_Fragment">


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

DetailsFragment XML file

<?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:id="@ id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/design_default_color_on_secondary"
    android:orientation="vertical"
    tools:context=".DetailsFragment">


    <TextView
        android:id="@ id/tvDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textview"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

</LinearLayout>

error

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.fragmentcheck, PID: 28112 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.fragmentcheck.MainActivity.onItemSelected(MainActivity.java:34) at com.example.fragmentcheck.list_Fragment.onListItemClick(list_Fragment.java:54)

CodePudding user response:

tvDescription is in the DetailsFragment layout, you need to do any layout configuration in the code for that Class

i.e.


class DetailsFragment extends Fragment {

...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        tvDescription = findViewById(R.id.tvDescription);
        tvDescription.text = "foobar"

        return (LinearLayout) inflater.inflate(R.layout.fragment_details, container, false);
    }

}

CodePudding user response:

You have tvDescription in DetailsFragment xml, while you are finding it's id in MainAcitvity. MainActivity just have FragmentContainerView , it doen't have any text view inside it.

You need to inflate the Fragment's view and call findViewById() on the View it returns.

Updated DetailsFragment

public class DetailsFragment extends Fragment {

    TextView tvDescription;
    public DetailsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_details, container, false)
          tvDescription = view.findViewById(R.id.tvDescription);
          tvDescription.setText("some text");
       
        return view
    }
}
  • Related