Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Android App NullPointer Exception Help

harleyquinJan 27 2022

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>

Comments

Answer

they are ALL locked until you either unlock or end the session.

Marked as Answer by Mubashar Iftikhar · Sep 27 2020
Mubashar Iftikhar

Hi Dear
I need some guidance.

Can you please share with me your email address ?

Dave Stokes-MySQL Community Team-Oracle

it would probably be better to get help from https://forums.mysql.com as i am travelling so much this time of year.  Our MySQL Oracle Enginners monitor the forums and will be fast to answer then I

Mubashar Iftikhar

Actually i need guideline of MYSQL 5.6 Exam , and at forms i dont think i can get better information .

I am no longer involved with the MySQL certification exams but can tell you I took the beta of the 5.6 DBA exam and found it very hard.   Another member of the MySQL Community Team at the time of the beta took the DBA and Developer exams and concurred that they were very hard.  The best advise is to go through the list of items covered on the exam and study your rear end off.

Good luck.

Mubashar Iftikhar

Dear Sir

      I know this exam is very hard because i have already attempted 2 times and not able to clear the exam. . & i have done my complete preparation from official documentation https://dev.mysql.com/doc/refman/5.6/en/preface.html plus 6 months practical work. Now i just trying to find out a way how to clear this exam because this task giving me a lot of headache.

Mubashar Iftikhar

Hi Dear

I am in search of open source data platform which can perform ETL over Big data (structured, semi-structured & un-structured) and analytical tools for data visualization to build analytical dashboards.

I will be grateful if you can share your choice of open source data platform and data visualization tools information & your use case high level information their key features and unique capabilities. Also relevant enablement resources/training options.

Looking forward for a healthy discussion over here.

Regards

Mubashar Iftikhar

Mubashar Iftikhar

Dear Dave Stokes,
I need some help.

I have two MySQL Database Servers i.e. One is Active Server --> Master and second is Passive Server --> Slave.

And as we know we have 3-4 MySQL HR,DR Solutions / Clustering options.For example

01 : MySQL NDB Cluster

02 : MySQL InnoDB Cluster

03 : MySQL Cluster Carrier Grade

My First Question is i want to deploy One Active & One Passive MySQL Server so which Cluster Option / HA,DR Option will be fit for above mentioned scenario ?

My Second Question is suppose i have deploy One Active & One Passive MySQL Server in any above mentioned Cluster Option / HA,DR Option. So what about licensing ? I have to buy license of MySQL Servers/ Cluster ?

Regards,

Mubashar Iftikhar

Please see your other question asked today.

1 - 9

Post Details

Added on Jan 27 2022
0 comments
587 views