gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:49:50 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com