尝试在空对象引用上调用虚方法'android.view.View android.support.v4.widget.NestedScrollView.findViewById(int)'

时间:2018-03-14 14:06:24

标签: java android android-fragments

我正在尝试将我的NestedScrollView放在我的MapFragment上,但是当我尝试时,会出现此错误:

  

尝试在空对象引用上调用虚拟方法'android.view.View android.support.v4.widget.NestedScrollView.findViewById(int)'

这是我的MapFragment.java文件

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();
  }
}

看起来像R.id.nestedScrollView返回NULL,但为什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

将此代码NestedScrollView nestedScrollView;移至顶部,并在onCreateView()方法中引用该代码。像这样:  nestedScrollView = (NestedScrollView) view.findViewById(R.id.bottom_sheet);。现在,您可以在片段中的任何方法中使用它,包括onMarkerClick()

您收到NPE错误,因为您的代码告诉Dalvik计算机它应该在空视图NestedScrollView中查找NestedScrollView nestedScrollView= null

希望它有所帮助。