Home > other >  Android Null Pointer Exception 2 Fragments 1 Activity?
Android Null Pointer Exception 2 Fragments 1 Activity?

Time:01-30

Hi I have a small android app that demonstrates 2 fragments in 1 activity, it should just be a simple code but I am getting a null pointer exception. The app is just a listview in 1 fragment, and an edittext in the other and it connects the main activity. My book is from 2016 so I think some of these things were depreciated that why I am having the error. The text should change in the other fragment after pressing the listview options. Also I am supposed to save the instance state but I haven't got that far yet. I believe the error is from the listener. OnFragmentInteractionListener that it points to null, I can comment the line. Thanks. Code below:

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 = (TextView)getActivity().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>

ListFragment.java
package com.example.tourguide;

import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
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;
    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);
        //listener= (MainActivity)getActivity();
        listView.setOnItemClickListener(this::onItemClick);
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int index, long id)
    {
        //if(listener != null)
        //{
            listener.onFragmentInteraction(index); //this is what points to null (the listener)
        //}
    }



    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>

MainActivity.java
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>

CodePudding user response:

if you want to use findById in fragment , you have to add the "view" before them , to add them to view which your returning

in your onCreateView change :

    textView = (TextView)getActivity().findViewById(R.id.textView1);

to :

    textView = view.findViewById(R.id.textView1);
  •  Tags:  
  • Related