为什么MapActivity在较低的API手机上崩溃?

时间:2016-06-06 23:48:34

标签: java android google-maps nullpointerexception crash

我的MapActivity在低API手机上崩溃了。在我的LG G4中,它并没有崩溃。为什么会这样?

这是我的代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private static final String FIREBASE_URL="https://***.firebaseio.com/";
private Firebase firebaseRef;
private LocationManager locationManager;
private GoogleMap mMap;
boolean bPermissionGranted;
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps2);
    Firebase.setAndroidContext(this);
    firebaseRef = new Firebase(FIREBASE_URL);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    setUpMapIfNeeded();
    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker marker) {
            final String title = marker.getTitle().toString();

            firebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot user : dataSnapshot.child("users").getChildren()) {
                        if (user.child("event/event_title").exists()) {
                            String event_title = user.child("event/event_title").getValue().toString();
                            if (title.equals(event_title)) {

                                String event_date = user.child("event/event_date").getValue().toString();
                                String event_content = user.child("event/event_content").getValue().toString();
                                String age_limit = user.child("event/age_limit").getValue().toString();
                                String event_hour = user.child("event/event_hour").getValue().toString();
                                String location_left = user.child("location_left").getValue().toString();
                                String location_right = user.child("location_right").getValue().toString();
                                String event_address=user.child("event_address").getValue().toString();
                                String club_name=user.child("club_name").getValue().toString();
                                final SharedPreferences sp = getSharedPreferences("key", 0);
                                final SharedPreferences.Editor sedt = sp.edit();
                                sedt.putString("event_address",event_address);
                                sedt.putString("event_title", event_title);
                                sedt.putString("event_date", event_date);
                                sedt.putString("event_content", event_content);
                                sedt.putString("age_limit", age_limit);
                                sedt.putString("event_hour", event_hour);
                                sedt.putString("location_left", location_left);
                                sedt.putString("location_right", location_right);
                                sedt.putString("club_name",club_name);
                                sedt.commit();

                            }

                        }
                    }
                    Intent intent = new Intent(MapsActivity.this, eventInfo.class);
                    startActivity(intent);
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {
                    Toast.makeText(MapsActivity.this, "Error", Toast.LENGTH_LONG).show();
                }
            });

    return true;
        }
    });

}


private void setUpMapIfNeeded() {
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        Log.w("myApp", "1");
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

        if (mMap != null) {

            Log.w("myApp", "2");
            CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(32.065483, 34.824550));
            CameraUpdate zoom = CameraUpdateFactory.zoomTo(10);
            mMap.moveCamera(center);
            mMap.animateCamera(zoom);

        }
    }
}


@Override
public void onMapReady(final GoogleMap googleMap) {

    mMap=googleMap;

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

    }
    else
    {
        Log.w("myApp", "3");
        mMap.setMyLocationEnabled(true);

    }

    firebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.w("myApp", "4");

                for (DataSnapshot child : dataSnapshot.child("users").getChildren()) {
                    if(child.child("event/event_title").exists()) {
                        if (dateRelevant(child.child("event/event_date").getValue().toString()) == true) {
                            String rightLocation = child.child("location_right").getValue().toString();
                            String leftLocation = child.child("location_left").getValue().toString();

                            double location_left = Double.parseDouble(leftLocation);
                            double location_right = Double.parseDouble(rightLocation);
                            String event_title = child.child("event/event_title").getValue().toString();
                            LatLng cod = new LatLng(location_left, location_right);
                            googleMap.addMarker(new MarkerOptions().position(cod).title(event_title));


                        }
                    }
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });


}

public boolean dateRelevant(String event_date) {


    Calendar now = Calendar.getInstance();
    int curr_year = now.get(Calendar.YEAR);
    int curr_month = now.get(Calendar.MONTH) + 1; // Note: zero based!
    int curr_day = now.get(Calendar.DAY_OF_MONTH);
    String curr_year2=curr_year+"";
    String [] esre = curr_year2.split("0");
    int esreYear=Integer.parseInt(esre[1]);
    int day;
    int month;
    int year;

    if(event_date.indexOf(".")!=-1) {
        String[] dateParts = event_date.split("\\.");
        day = Integer.parseInt(dateParts[0]);
        month = Integer.parseInt(dateParts[1]);
        year = Integer.parseInt(dateParts[2]);
    }
    else
    {
        String[] dateParts = event_date.split("/");
        day = Integer.parseInt(dateParts[0]);
        month = Integer.parseInt(dateParts[1]);
        year = Integer.parseInt(dateParts[2]);
    }

    if(year<esreYear||(curr_year>year&&year!=16&&year!=17)) {
        return false;

    }
    else
    {
        if(year==curr_year||year==esreYear)
        {
            if(month==curr_month)
            {
                if(day>=curr_day)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                if(month>curr_month)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        else
        {
            if(esreYear<year||curr_year<year)
            {
                return true;
            }
            else {
                return false;
            }
        }
    }

}
}

logcat中的错误说:

  

致命的例外:主要                                                                            过程:com.giladneiger.clubber,PID:22389                                                                            java.lang.RuntimeException:无法启动活动   ComponentInfo {com.giladneiger.clubber / com.giladneiger.clubber.MapsActivity}:   显示java.lang.NullPointerException                                                                                在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)

1 个答案:

答案 0 :(得分:1)

您正在使用public class EditTextEnabledValueConverter : MvxValueConverter<bool, InputTypes> { protected override InputTypes Convert(bool value, Type targetType, object parameter, CultureInfo culture) { if (value) return InputTypes.ClassNumber | InputTypes.NumberFlagDecimal | InputTypes.NumberFlagSigned; return InputTypes.Null; } } ,但是您要在<EditText style="@style/InputNumbersEditText" android:layout_weight="1" android:layout_width="0dp" android:focusable="true" android:layout_height="wrap_content" local:MvxBind="Text Age; InputType EditTextEnabled(MyProperty)" /> 上设置地图。如果地图尚未就绪,您将收到OnMapReadyCallback

将所有初始化代码移至onCreate方法。

您的代码如下所示:

NullPointerexception