受尊敬的会员,我正在开发基本的Google地图应用程序。我已经为这个应用程序实现了一些功能,如GPS检查,互联网检查,工作互联网检查等。我刚刚在顶部添加了一个选项菜单,其中包含一个SATELLITE TYPE地图。我已经写了正确的代码,但每当我点击SATELLITE选项来更改我的地图视图时,应用程序就会自动关闭。终端中没有显示错误。应用程序适用于所有其他事情。我无法弄清楚我做错了哪个关闭我的应用而不是加载特定的地图类型。 以下是我的main_menu XML资源文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/SatelliteType"
android:title="@string/satellite"/>
</menu>
以下是Java代码。如果我需要更清楚地阐述这些内容以了解我在编码实践中的错误/缺点,请告诉我:
package com.example.Test;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
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 java.net.InetAddress;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap googleMap;
private static final int ERROR_DIALOG_REQUEST = 9901;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
//****************************Checking the GPS at start*******************
final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS is Enabled in your device", Toast.LENGTH_SHORT).show();
} else {
showGPSDisabledAlertToUser();
}
//********Checking the Google Play Services at start*********
if (ServicesOK()) {
Toast.makeText(this, "CONNECTED to Google Play Services", Toast.LENGTH_SHORT).show();
}
//****************************Checking the NETWORKING DEVICE at start*******************
if (isConnected()) {
Toast.makeText(this, "CONNECTED to NETWORKING DEVICE", Toast.LENGTH_SHORT).show();
}
//****************************Checking the INTERNET SERVICE at Start*******************
if (isInternetAvailable()) {
Toast.makeText(this, "CONNECTED to INTERNET", Toast.LENGTH_SHORT).show();
}
// 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);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//****************************When MAP is perfectly LOADED*******************
@Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in CIITLAHORE and move the camera
LatLng ciitlahore = new LatLng(31.400693, 74.210941);
googleMap.addMarker(new MarkerOptions().position(ciitlahore).title("Marker in CIIT LAHORE"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(ciitlahore));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//****************************Making MENU Option******************
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//Add menu handling code
switch (id) {
case R.id.SatelliteType:
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
return (true);
default :
return super.onOptionsItemSelected(item);
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//****************************METHOD for checking GOOGLE PLAY SERVICES*************************
public boolean ServicesOK() {
int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
if (resultCode == ConnectionResult.SUCCESS) {
return true;
} else {
GoogleApiAvailability.getInstance().getErrorDialog(this, resultCode, ERROR_DIALOG_REQUEST).show();
Toast.makeText(this, "Can't connect to mapping services", Toast.LENGTH_SHORT).show();
}
return false;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//****************************METHOD for checking INTERNET AVAILABLE*************************
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com");
return !ipAddr.equals("");
} catch (Exception e) {
return false;
}
}
//****************************METHOD for checking INTERNET CONNECTIVITY*************************
public boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
// connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
Toast.makeText(this, "WIFI CONNECTION AVAILABLE", Toast.LENGTH_SHORT).show();
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
Toast.makeText(this, "CELLULAR CONNECTION AVAILABLE", Toast.LENGTH_SHORT).show();
}
} else {
// not connected to the internet
Toast.makeText(this, "NOT CONNECTED to any working INTERNET service", Toast.LENGTH_SHORT).show();
}
return false;
}
//****************************GPS DIALOGUE BOX*************************
private void showGPSDisabledAlertToUser() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
finishAffinity();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++ END +++++++++++++++++++++++++++++++++++++++++++++++
}
答案 0 :(得分:3)
您的googleMap
对象为null
。您需要将其添加到onMapReady
方法:
this.googleMap = googleMap;
此外,当您选择菜单时,googleMap
对象可能是null
,因此您需要检查它:
if (googleMap != null) {
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
答案 1 :(得分:1)
始终检查地图对象是否为空。在您的代码中,未正确启动googleMap对象以检查NULL。这就是为什么当您选择其他地图类型时,您的应用会一次又一次地崩溃