skip to Main Content

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));
    }}

list_fragment

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

<?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

<?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 r*emphasized text*eference
        at com.example.fragmentcheck.MainActivity.onItemSelected(MainActivity.java:34)
        at com.example.fragmentcheck.list_Fragment.onListItemClick(list_Fragment.java:54)

3

Answers


  1. 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) {
    
            View view = inflater.inflate(R.layout.fragment_details, container, false)
              tvDescription = view.findViewById(R.id.tvDescription);
              tvDescription.setText("some text");
           
            return view
        }
    
    }
    
    
    Login or Signup to reply.
  2. 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
        }
    }
    
    Login or Signup to reply.
  3. Change both: androidx.fragment.app.FragmentContainerView

    To: fragment

    In the main_activity xml

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search