Discussions
Categories
- 196.7K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.9K Databases
- 221.6K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.9K SQL & PL/SQL
- 21.3K SQL Developer
- 295.4K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.1K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 153 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 398 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 170 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 230 Portuguese
Android App NullPointer Exception Help

harleyquin
Member Posts: 2 Green Ribbon
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. The textview should update when an item in the listview is selected but it is not updating, it is crashing now. I think it is a nullpointer exception after commenting out the if( !=null), the listener.OnFragmentInteraction does not get the index, it says that the listener points to null, I'm learning from an older video series and some things were depreciated, seeking help please thanks.
fragment_text.xml <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/TextFragment_layout1" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".TextFragment"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="16dp" android:text="@string/welcome" /> </FrameLayout> TextFragment.java package com.example.tourguide; import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; /** * A simple {@link Fragment} subclass. * Use the {@link TextFragment#newInstance} factory method to * create an instance of this fragment. */ public class TextFragment extends Fragment { TextView textView; // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; public TextFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment TextFragment. */ // TODO: Rename and change types and number of parameters public static TextFragment newInstance(String param1, String param2) { TextFragment fragment = new TextFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_text, container, false); textView = view.findViewById(R.id.textView1); return view; } public void updateTextView(int index) { if(index==0) { textView.setText(getResources().getString(R.string.set_Richmond)); } else if(index==1) { textView.setText(getResources().getString(R.string.set_Vancouver)); } else if(index==2) { textView.setText(getResources().getString(R.string.set_Toronto)); } else if(index==3) { textView.setText(getResources().getString(R.string.set_New_West)); } else if(index==4) { textView.setText(getResources().getString(R.string.set_Montreal)); } else if(index==5) { textView.setText(getResources().getString(R.string.set_Aldergrove)); } } public interface OnFragmentInteractionListener { public void onFragmentInteraction(int index); } } fragment_list.xml <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ListFragment_layout1" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ListFragment"> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:dividerHeight="32dp" /> </FrameLayout> package com.example.tourguide; import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.AdapterView; import android.widget.ListView; /** * A simple {@link Fragment} subclass. * Use the {@link ListFragment#newInstance} factory method to * create an instance of this fragment. */ public class ListFragment extends Fragment implements AdapterView.OnItemClickListener { ListView listView; String[] array; String listStr; private OnFragmentInteractionListener listener; // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; public ListFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment ListFragment. */ // TODO: Rename and change types and number of parameters public static ListFragment newInstance(String param1, String param2) { ListFragment fragment = new ListFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_list, container, false); listView = view.findViewById(R.id.listView1); array = getResources().getStringArray(R.array.string_array_cities); ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(), android.R.layout.simple_list_item_1, array); listView.setAdapter(adapter); listView.setOnItemClickListener(this); return view; } @Override public void onItemClick(AdapterView<?> adapterView, View view, int index, long id) { if(listener != null) { listener.onFragmentInteraction(index); //pass index of array } } public interface OnFragmentInteractionListener { public void onFragmentInteraction(int index); } } activity_main.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_1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.fragment.app.FragmentContainerView android:id="@+id/fragmentContainerView1_list" android:name="com.example.tourguide.ListFragment" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" tools:layout="@layout/fragment_list" /> <androidx.fragment.app.FragmentContainerView android:id="@+id/fragmentContainerView2_text" android:name="com.example.tourguide.TextFragment" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" tools:layout="@layout/fragment_text" /> </LinearLayout> package com.example.tourguide; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentManager; import android.app.Fragment; import android.net.Uri; import android.os.Bundle; public class MainActivity extends AppCompatActivity implements TextFragment.OnFragmentInteractionListener, ListFragment.OnFragmentInteractionListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onFragmentInteraction(int index) { FragmentManager manager = getSupportFragmentManager(); TextFragment textFragment = (TextFragment)manager.findFragmentById(R.id.fragmentContainerView2_text); textFragment.updateTextView(index); } } strings.xml <resources> <string name="app_name">Tour Guide</string> <string name="hello_blank_fragment">Hello blank fragment</string> <string name="text_box">default</string> <string name="welcome">Welcome to the Tour Guide App</string> <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 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> <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> <string name="set_Toronto">Toronto is Canadas biggest city on the east coast and home to the TSX and Maple Leafs.</string> <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. Queens Park home of the New West Royals.</string> <string name="set_Montreal">Monteal is famous for their smoked meat and they speak french.</string> <string name="set_Aldergrove">Aldergrove is a quite town and is east of Langley not too far and is a nice place.</string> <string-array name="string_array_cities"> <item>Richmond</item> <item>Vancouver</item> <item>Toronto</item> <item>New West</item> <item>Montreal</item> <item>Aldergrove</item> </string-array> </resources>