Android应用程序崩溃了所有其他编译/启动

时间:2016-01-18 04:45:33

标签: java android

出于某种原因我每次编译我的应用程序时,应用程序崩溃,我收到以下错误。

 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.clear()' on a null object reference
        at com.mycompany.talk.Fragments.MapFragment$1.onLocationChanged(MapFragment.java:68)

我不确定如何解决此错误以及导致错误的原因。它适用于某些编辑和启动,但在其他编辑和应用程序崩溃。错误发生在我的" MapFragment"类。

onLocationChanged(Location)方法。

onLocationChanged()

        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {

                /*
                Updates to our map may need to be taken place here. Need to listen to other devices in the area.
                 */

                Log.e("Latitude: ", "" + location.getLatitude());
                Log.e("Longitude: ", "" + location.getLongitude());
                maps.clear(); //Clear the map of any  existing markers
                mLatLng = new LatLng(location.getLatitude(),location.getLongitude());
                MarkerOptions mp1 = new MarkerOptions();//Instantiate a new "MarkerOptions" where we will be able to define a...
                                                        //...marker

                mp1.position(mLatLng);
                mp1.title("You");
                maps.addMarker(mp1);//Finally add the marker to the map
                maps.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));//Move camera to markers location using our "latLng" variable
                maps.animateCamera(CameraUpdateFactory.zoomTo(20));// Zoom, (between 2.0 - 21.0) the higher, the more zoomed in

            }
        }

MapFragment.java

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.mycompany.talk.R;


public class MapFragment extends Fragment implements OnMapReadyCallback{

SupportMapFragment mSupportMapFragment;
int radius = 20;
private GoogleMap maps;
LatLng mLatLng;
LocationManager mLocationManager;
LocationListener mLocationListener;

public LatLng getLatLng(){
    return mLatLng;
}

@Override
public void onStart(){
    super.onStart();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    mSupportMapFragment = SupportMapFragment.newInstance();
    android.support.v4.app.FragmentManager sfm = getFragmentManager();
    mSupportMapFragment.getMapAsync(this);
    if(!mSupportMapFragment.isAdded())
    sfm.beginTransaction().add(R.id.map_frag,mSupportMapFragment).commit();

   else if(mSupportMapFragment.isAdded())
        sfm.beginTransaction().hide(mSupportMapFragment).commit();
    else
        sfm.beginTransaction().show(mSupportMapFragment).commit();

    //LocationManager mLocationManager;
   // LocationListener mLocationListener;
    mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {

                /*
                Updates to our map may need to be taken place here. Need to listen to other devices in the area.
                 */

                Log.e("Latitude: ", "" + location.getLatitude());
                Log.e("Longitude: ", "" + location.getLongitude());

                if(maps != null) {
                    maps.clear(); //Clear the map of any  existing markers
                }
                mLatLng = new LatLng(location.getLatitude(),location.getLongitude());//This is an Update!!!!!!!!! Might not work!!!!!
                MarkerOptions mp1 = new MarkerOptions();//Instantiate a new "MarkerOptions" where we will be able to define a...
                                                        //...marker

                mp1.position(mLatLng);//This is an Update!!!!!!!!! Might not work!!!!!
                mp1.title("You");
                maps.addMarker(mp1);//Finally add the marker to the map
                maps.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));//Move camera to markers location using our "latLng" variable
                maps.animateCamera(CameraUpdateFactory.zoomTo(20));// Zoom, (between 2.0 - 21.0) the higher, the more zoomed in

            }
        }
        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }
        @Override
        public void onProviderEnabled(String s) {

        }
        @Override
        public void onProviderDisabled(String s) {

        }
    };

    mLocationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mLocationListener);//THIS IS AN UPDATE!
    //Need more precise coordinates..............
    return inflater.inflate(R.layout.fragment_map, container, false);
}

@Override
public void onMapReady(GoogleMap map) {
    maps = map;
}

@Override
public void onResume(){
    Log.e("RESUME","onResume called");
    super.onResume();
}
@Override
public void onStop(){
    mLocationManager.removeUpdates(mLocationListener);
    Log.e("PAUSE", "Coordinates stopped");
    super.onStop();
}

@Override
public void onPause(){
    mLocationManager.removeUpdates(mLocationListener);
    Log.e("PAUSE", "Coordinates stopped");
    super.onPause();
 }
}

1 个答案:

答案 0 :(得分:1)

初始化maps并使用两种不同的回调方法onLocationChanged(...)onMapReady(...)进行访问,这意味着如果在地图准备好之前位置发生变化(onLocationChanged(...)之前调用onMapReady(...)),maps将为null。要解决此问题,您可以在访问maps之前检查null是否为@Override public void onLocationChanged(Location location) { if (location != null && maps != null) { .... } } 。例如:

Private Sub RtfToWordDoc()

    ' Connect to database
    Dim sqlCon As New SqlConnection(strConnectionString)
    Dim sqlCmd As New SqlCommand
    Dim sqlReader As SqlDataReader
    Dim oWord As New Word.Application

    ' prepare the sql query
    sqlCmd.CommandText = "select ... from ... where ..."
    sqlCmd.CommandType = CommandType.Text
    sqlCmd.Connection = sqlCon

    ' obtain the query results
    sqlCon.Open()
    sqlReader = sqlCmd.ExecuteReader
    sqlReader.Read()

    If sqlReader.HasRows Then

        ' Populate the RichTextBox control on the Form with Query Results 
        Me.richTextBoxEx1.Rtf = CStr(sqlReader.GetSqlString(1))

        ' Copy the rich text box content
        richTextBoxEx1.SelectionStart = 0
        richTextBoxEx1.SelectionLength = richTextBoxEx1.TextLength
        richTextBoxEx1.Copy()

        ' Create a word document & paste the rich text box content into it
        oWord.Documents.Add()
        oWord.Visible = True
        Dim range = oWord.ActiveDocument.Range(Start:=0, End:=0)
        range.Paste()   ' keeps RTB formatting, ignores word formatting

    Else
        MsgBox("No results found.", MsgBoxStyle.OkOnly, "Error")
    End If

    sqlCon.Close()

End Sub