-3

I'm trying to put my NestedScrollView on my MapFragment, but when I'm trying to, this error appears :

Attempt to invoke virtual method 'android.view.View android.support.v4.widget.NestedScrollView.findViewById(int)' on a null object reference

Here is my MapFragment.java file

package com.example.alexandre.list.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.alexandre.list.MainActivity;
import com.example.alexandre.list.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link OnMapClickListener} interface
 * to handle interaction events.
 * Use the {@link MapFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MapFragment extends Fragment implements OnMapReadyCallback {
    // 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;

private GoogleMap mGoogleMap;
private MapView mMapView;

private OnMapClickListener mListener;

public MapFragment() {
    // 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 MapFragment.
 */
// TODO: Rename and change types and number of parameters
public static MapFragment newInstance(String param1, String param2) {
    MapFragment fragment = new MapFragment();
    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) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_map, container, false);
    bindViews(view, savedInstanceState);
    return view;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed() {
    if (mListener != null) {
        mListener.onMapClick();
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnMapClickListener) {
        mListener = (OnMapClickListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnTweetListClickListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

private void bindViews(View view, Bundle savedInstanceState) {
    mMapView = view.findViewById(R.id.map);
    mMapView.onCreate(savedInstanceState);
    mMapView.getMapAsync(this);
}

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;




    LatLng castlePos = new LatLng(45.209299, 5.659144);
    LatLng treePos = new LatLng(MainActivity.posx, MainActivity.posy);
    CameraPosition liberty = CameraPosition.builder().target(castlePos).zoom(17)/*.bearing(0).tilt(0)*/.build();
    Marker marker = mGoogleMap.addMarker(new MarkerOptions()
                                .position(treePos)
                                .title("Test du cèdre du Liban")
                                .snippet("Libani"));
    marker.hideInfoWindow();
    mGoogleMap.setOnMarkerClickListener(
            new GoogleMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
                    ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
                    NestedScrollView nestedScrollView = null;
                    nestedScrollView = (NestedScrollView) nestedScrollView.findViewById(R.id.bottom_sheet);
                    nestedScrollView.setVisibility(View.VISIBLE);

                    return false;
                }
            }
    );
    mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(liberty));
    mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnMapClickListener {
    void onMapClick();
  }
}

It looks like R.id.nestedScrollView return NULL, but why ?

Thanks !

  • 1
    These lines are the root cause of your problem: `NestedScrollView nestedScrollView = null; nestedScrollView = (NestedScrollView) nestedScrollView.findViewById(R.id.bottom_sheet);` You set it to null, then call `findViewById()` on it. – Shark Mar 14 '18 at 14:13

1 Answers1

0

Move this code NestedScrollView nestedScrollView; to the top, and reference it in your onCreateView() method. Like this: nestedScrollView = (NestedScrollView) view.findViewById(R.id.bottom_sheet);. Now you can use it in any method in the fragment including your onMarkerClick().

You're getting the NPE error because your code is telling the Dalvik Machine that it should look for NestedScrollView in a null view NestedScrollView nestedScrollView= null.

Hope it helps.

devmike01
  • 1,971
  • 1
  • 20
  • 30