2016-06-06 44 views
0

我的MapActivity在低API手機上崩潰。在我的LG G4中,它並沒有崩潰。爲什麼會發生?爲什麼MapActivity在較低的API電話上崩潰?

這是我的代碼:

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)

+0

如何API的低是它崩潰的?你正在使用哪個版本的FragmentActivity? –

回答

1

您正在使用OnMapReadyCallback但您正在onCreate設置你的地圖。如果地圖尚未準備好,您將收到NullPointerexception

將所有初始化代碼移動到onMapReady方法。

您的代碼應該是這樣的:

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

// Remove the setUpMapIfNeeded method 

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

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

    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { 
     // ... 
    }); 

    // ... 
}