1. Are you ready for the Galaxy S20? Here is everything we know so far!

Null Pointer Exception Help in Code

Discussion in 'Android Development' started by ilyas khan, Jan 26, 2022.

  1. ilyas khan

    ilyas khan Lurker
    Thread Starter

    Hey
    The is the code for my android app it is a app that demonstrates how to communicate with multiple fragments in one activity. But the textview in the text fragment is not being updated after an item of the listview is selected because of a nullpointer exception from the listener I think. The textview should update when an item in the listview is selected but it is not updating, it is not crashing though. Can someone help? Thanks.

    fragment_text.xml
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     xmlns:tools="http://schemas.android.com/tools"
    4.     android:id="@+id/TextFragment_layout1"
    5.     android:layout_width="match_parent"
    6.     android:layout_height="match_parent"
    7.     tools:context=".TextFragment">
    8.  
    9.  
    10.     <TextView
    11.         android:id="@+id/textView1"
    12.         android:layout_width="wrap_content"
    13.         android:layout_height="wrap_content"
    14.         android:padding="16dp"
    15.         android:text="[USER=696546]@String[/USER]/welcome" />
    16.  
    17. </FrameLayout>
    18.  
    Code (Java):
    1. package com.example.tourguide;
    2.  
    3. import android.content.res.Resources;
    4. import android.net.Uri;
    5. import android.os.Bundle;
    6.  
    7. import androidx.fragment.app.Fragment;
    8.  
    9. import android.view.LayoutInflater;
    10. import android.view.View;
    11. import android.view.ViewGroup;
    12. import android.widget.ArrayAdapter;
    13. import android.widget.ListView;
    14. import android.widget.TextView;
    15.  
    16. /**
    17. * A simple {@link Fragment} subclass.
    18. * Use the {@link TextFragment#newInstance} factory method to
    19. * create an instance of this fragment.
    20. */
    21. public class TextFragment extends Fragment
    22. {
    23.     TextView textView;
    24.  
    25.  
    26.     // TODO: Rename parameter arguments, choose names that match
    27.     // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    28.     private static final String ARG_PARAM1 = "param1";
    29.     private static final String ARG_PARAM2 = "param2";
    30.  
    31.     // TODO: Rename and change types of parameters
    32.     private String mParam1;
    33.     private String mParam2;
    34.  
    35.     public TextFragment() {
    36.         // Required empty public constructor
    37.     }
    38.  
    39.     /**
    40.      * Use this factory method to create a new instance of
    41.      * this fragment using the provided parameters.
    42.      *
    43.      * [USER=315340]@param[/USER] param1 Parameter 1.
    44.      * [USER=315340]@param[/USER] param2 Parameter 2.
    45.      * @return A new instance of fragment TextFragment.
    46.      */
    47.     // TODO: Rename and change types and number of parameters
    48.     public static TextFragment newInstance(String param1, String param2) {
    49.         TextFragment fragment = new TextFragment();
    50.         Bundle args = new Bundle();
    51.         args.putString(ARG_PARAM1, param1);
    52.         args.putString(ARG_PARAM2, param2);
    53.         fragment.setArguments(args);
    54.         return fragment;
    55.     }
    56.  
    57.     [USER=1021285]@override[/USER]
    58.     public void onCreate(Bundle savedInstanceState)
    59.     {
    60.         super.onCreate(savedInstanceState);
    61.         if (getArguments() != null)
    62.         {
    63.             mParam1 = getArguments().getString(ARG_PARAM1);
    64.             mParam2 = getArguments().getString(ARG_PARAM2);
    65.         }
    66.  
    67.     }
    68.  
    69.     [USER=1021285]@override[/USER]
    70.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    71.                              Bundle savedInstanceState)
    72.     {
    73.         View view = inflater.inflate(R.layout.fragment_text, container, false);
    74.         textView = (TextView)getActivity().findViewById(R.id.textView1);
    75.         return view;
    76.     }
    77.  
    78.     public void updateTextView(int index)
    79.     {
    80.         if(index==0)
    81.         {
    82.             textView.setText(getResources().getString(R.string.set_Richmond));
    83.         }
    84.         else if(index==1)
    85.         {
    86.             textView.setText(getResources().getString(R.string.set_Vancouver));
    87.         }
    88.         else if(index==2)
    89.         {
    90.             textView.setText(getResources().getString(R.string.set_Toronto));
    91.         }
    92.         else if(index==3)
    93.         {
    94.             textView.setText(getResources().getString(R.string.set_New_West));
    95.         }
    96.         else if(index==4)
    97.         {
    98.             textView.setText(getResources().getString(R.string.set_Montreal));
    99.         }
    100.         else if(index==5)
    101.         {
    102.             textView.setText(getResources().getString(R.string.set_Aldergrove));
    103.         }
    104.     }
    105.  
    106.         public interface OnFragmentInteractionListener
    107.     {
    108.         public void onFragmentInteraction(int index);
    109.     }
    110. }

    Code (Text):
    1. fragment_list.xml
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:tools="http://schemas.android.com/tools"
    5.     android:id="@+id/ListFragment_layout1"
    6.     android:layout_width="match_parent"
    7.     android:layout_height="match_parent"
    8.     tools:context=".ListFragment">
    9.  
    10.     <ListView
    11.         android:id="@+id/listView1"
    12.         android:layout_width="match_parent"
    13.         android:layout_height="match_parent"
    14.         android:dividerHeight="32dp" />
    15.  
    16. </FrameLayout>


    Code (Java):
    1. package com.example.tourguide;
    2.  
    3. import android.content.res.Resources;
    4. import android.net.Uri;
    5. import android.os.Bundle;
    6.  
    7. import androidx.fragment.app.Fragment;
    8.  
    9. import android.view.LayoutInflater;
    10. import android.view.View;
    11. import android.view.ViewGroup;
    12. import android.widget.ArrayAdapter;
    13. import android.widget.AdapterView;
    14.  
    15. import android.widget.ListView;
    16.  
    17. /**
    18. * A simple {@link Fragment} subclass.
    19. * Use the {@link ListFragment#newInstance} factory method to
    20. * create an instance of this fragment.
    21. */
    22. public class ListFragment extends Fragment implements AdapterView.OnItemClickListener
    23. {
    24.     ListView listView;
    25.     String[] array;
    26.     String listStr;
    27.     OnFragmentInteractionListener listener;
    28.  
    29.  
    30.     // TODO: Rename parameter arguments, choose names that match
    31.     // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    32.     private static final String ARG_PARAM1 = "param1";
    33.     private static final String ARG_PARAM2 = "param2";
    34.  
    35.     // TODO: Rename and change types of parameters
    36.     private String mParam1;
    37.     private String mParam2;
    38.  
    39.     public ListFragment() {
    40.         // Required empty public constructor
    41.     }
    42.  
    43.     /**
    44.      * Use this factory method to create a new instance of
    45.      * this fragment using the provided parameters.
    46.      *
    47.      * [USER=315340]@param[/USER] param1 Parameter 1.
    48.      * [USER=315340]@param[/USER] param2 Parameter 2.
    49.      * @return A new instance of fragment ListFragment.
    50.      */
    51.     // TODO: Rename and change types and number of parameters
    52.     public static ListFragment newInstance(String param1, String param2) {
    53.         ListFragment fragment = new ListFragment();
    54.         Bundle args = new Bundle();
    55.         args.putString(ARG_PARAM1, param1);
    56.         args.putString(ARG_PARAM2, param2);
    57.         fragment.setArguments(args);
    58.         return fragment;
    59.     }
    60.  
    61.     [USER=1021285]@override[/USER]
    62.     public void onCreate(Bundle savedInstanceState) {
    63.         super.onCreate(savedInstanceState);
    64.         if (getArguments() != null)
    65.         {
    66.             mParam1 = getArguments().getString(ARG_PARAM1);
    67.             mParam2 = getArguments().getString(ARG_PARAM2);
    68.         }
    69.     }
    70.  
    71.     [USER=1021285]@override[/USER]
    72.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    73.     {
    74.         View view = inflater.inflate(R.layout.fragment_list, container, false);
    75.         listView = view.findViewById(R.id.listView1);
    76.         array = getResources().getStringArray(R.array.string_array_cities);
    77.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
    78.         listView.setAdapter(adapter);
    79.         listView.setOnItemClickListener(this::eek:nItemClick);
    80.         return view;
    81.     }
    82.  
    83.     [USER=1021285]@override[/USER]
    84.     public void onItemClick(AdapterView<?> adapterView, View view, int index, long id)
    85.     {
    86.         //if(listener != null)
    87.         //{
    88.             listener.onFragmentInteraction(index); //pass index of array
    89.         //}
    90.     }
    91.  
    92.     public interface OnFragmentInteractionListener
    93.     {
    94.         public void onFragmentInteraction(int index);
    95.     }
    96. }

    activity_main.xml
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     xmlns:app="http://schemas.android.com/apk/res-auto"
    4.     xmlns:tools="http://schemas.android.com/tools"
    5.     android:id="@+id/LinearLayout_1"
    6.     android:eek:rientation="vertical"
    7.     android:layout_width="match_parent"
    8.     android:layout_height="match_parent"
    9.     tools:context=".MainActivity">
    10.  
    11.     <androidx.fragment.app.FragmentContainerView
    12.         android:id="@+id/fragmentContainerView1_list"
    13.         android:name="com.example.tourguide.ListFragment"
    14.         android:layout_width="wrap_content"
    15.         android:layout_height="0dp"
    16.         android:layout_weight="1"
    17.         tools:layout="@layout/fragment_list" />
    18.  
    19.     <androidx.fragment.app.FragmentContainerView
    20.         android:id="@+id/fragmentContainerView2_text"
    21.         android:name="com.example.tourguide.TextFragment"
    22.         android:layout_width="wrap_content"
    23.         android:layout_height="0dp"
    24.         android:layout_weight="1"
    25.         tools:layout="@layout/fragment_text" />
    26. </LinearLayout>

    Code (Java):
    1. package com.example.tourguide;
    2.  
    3. import androidx.appcompat.app.AppCompatActivity;
    4. import androidx.fragment.app.FragmentManager;
    5.  
    6. import android.app.Fragment;
    7. import android.net.Uri;
    8. import android.os.Bundle;
    9.  
    10. public class MainActivity extends AppCompatActivity implements TextFragment.OnFragmentInteractionListener, ListFragment.OnFragmentInteractionListener
    11. {
    12.  
    13.     [USER=1021285]@override[/USER]
    14.     protected void onCreate(Bundle savedInstanceState)
    15.     {
    16.         super.onCreate(savedInstanceState);
    17.         setContentView(R.layout.activity_main);
    18.     }
    19.  
    20.  
    21.     [USER=1021285]@override[/USER]
    22.     public void onFragmentInteraction(int index)
    23.     {
    24.         FragmentManager manager = getSupportFragmentManager();
    25.         TextFragment textFragment = (TextFragment)manager.findFragmentById(R.id.fragmentContainerView2_text);
    26.         textFragment.updateTextView(index);
    27.     }
    28.  
    29. }
    -----
    strings.xml
    Code (Text):
    1. <resources>
    2.     <string name="app_name">Tour Guide</string>
    3.     <string name="hello_blank_fragment">Hello blank fragment</string>
    4.     <string name="text_box">default</string>
    5.     <string name="welcome">Welcome to the Tour Guide App</string>
    6.  
    7.     <string name="set_Richmond">Richmond is a quite city with many nice hidden scenic places. A small town named Steveston is found on the south west part
    8.     of the city and is well known for having good seafood and nice places to visit like Garry Point and the west and south dykes. </string>
    9.  
    10.     <string name="set_Vancouver">Vancouver is one of Canadas biggest cities and has a lot to offer like the malls and many towers, beaches, parks.</string>
    11.  
    12.     <string name="set_Toronto">Toronto is Canadas biggest city on the east coast and home to the TSX and Maple Leafs.</string>
    13.  
    14.     <string name="set_New_West">New West is a city east of Vancouver but close by and has nice parks and ice rinks like Moody Park and Queens Park.
    15.     Queens Park home of the New West Royals.</string>
    16.  
    17.     <string name="set_Montreal">Monteal is famous for their smoked meat and they speak french.</string>
    18.  
    19.     <string name="set_Aldergrove">Aldergrove is a quite town and is east of Langley not too far and is a nice place.</string>
    20.  
    21.     <string-array name="string_array_cities">
    22.         <item>Richmond</item>
    23.         <item>Vancouver</item>
    24.         <item>Toronto</item>
    25.         <item>New West</item>
    26.         <item>Montreal</item>
    27.         <item>Aldergrove</item>
    28.     </string-array>
    29.  
    30.  
    31. </resources>
     


    #1 ilyas khan, Jan 26, 2022
    Last edited by a moderator: Jan 26, 2022

    1. Download the Forums for Android™ app!


      Download

       
  2. Unforgiven

    Unforgiven ...eschew obfuscation...
    Moderator

    I moved this to the development area for better exposure. I also added code tags to make it more readable for the folks that can help. :)
     
    ilyas khan likes this.
  3. Unforgiven

    Unforgiven ...eschew obfuscation...
    Moderator

    Apologies if I broke up the code snippets incorrectly, feel free to use the edit post thread to correct them. :)
     
    ilyas khan likes this.
Loading...

Share This Page

Loading...