作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 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 中的错误显示:
FATAL EXCEPTION: main Process: com.giladneiger.clubber, PID: 22389 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.giladneiger.clubber/com.giladneiger.clubber.MapsActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
最佳答案
您正在使用 OnMapReadyCallback
,但您正在 onCreate
上设置 map 。如果 map 尚未准备好,您将收到 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() {
// ...
});
// ...
}
关于java - 为什么 MapActivity 在 API 较低的手机上崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37668484/
我是一名优秀的程序员,十分优秀!