我已将我的活动设置为自动设置位置,并且在未启用GPS时我会显示一个对话框。该应用程序的工作原理以及设备GPS获取位置数据,但是当没有GPS数据时应用程序崩溃。
这是我的代码
public class Newtab extends FragmentActivity {
String lat;
String longi;
String Name;
String cate;
private Map<Marker, Class> allMarkersMap = new HashMap<Marker, Class>();
private static final String TAG = MainActivity.class.getSimpleName();
/////Main activity/////////
public void mainactivity(View view)
{
Intent intent = new Intent(Newtab.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
//////About//////
public void about(View view)
{
Intent intent = new Intent(Newtab.this, About.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
private ProgressDialog pDialog;
private ListView listView;
private CustomListAdapter adapter1;
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
private GoogleMap mMap;
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
setUpMapIfNeeded();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager!=null){
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Toast.makeText(this, "GPS Data pending", Toast.LENGTH_SHORT).show();
Global.able= 1;
}else{
showGPSDisabledAlertToUser();
Global.able=0;
}
//////////////////////////////////////////////////////////////////////////////////////
if (Global.itemsl!=null){
// Parsing json
for (int i = 0; i < Global.itemsl.length(); i++) {
try {
final JSONObject obj = Global.itemsl.getJSONObject(i);
final Movie movie = new Movie();
movie.setCATEGORY(obj.getString("CATEGORY"));
lat= movie.setLATITUDE(obj.getString("LATITUDE"));
longi= movie.setLATITUDE(obj.getString("LONGITUDE"));
Name = movie.setNAME(obj.getString("NAME"));
cate = movie.setCATEGORY(obj.getString("CATEGORY"));
double latt=Double.parseDouble(lat);
double longg=Double.parseDouble(longi);
if(Global.able==0){
mMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(36.1911,44.0092) , 6) );
}
final MarkerOptions marker = new MarkerOptions().position(new LatLng(latt, longg)).title(obj.getString("NAME"));
mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
public void onInfoWindowClick(Marker marker) {
}
@Override
public boolean onMarkerClick(Marker arg0) {
String test=marker.getTitle();
for (int i = 0; i < Global.itemsl.length(); i++) {
try {
JSONObject obj = Global.itemsl.getJSONObject(i);
Name = movie.setNAME(obj.getString("NAME"));
String DESCRIPTION = movie.setNAME(obj.getString("DESCRIPTION"));
String IMAGELINK = movie.setNAME(obj.getString("IMAGELINK"));
String PHONES = movie.setPHONES(obj.getString("PHONES"));
String EMAILS = movie.setEMAILS(obj.getString("EMAILS"));
String ADDRESS = movie.setADDRESS(obj.getString("ADDRESS"));
String LATITUDE = movie.setLATITUDE(obj.getString("LATITUDE"));
String LONGITUDE = movie.setLONGITUDE(obj.getString("LONGITUDE"));
String FACEBOOK= movie.setFACEBOOK(obj.getString("FACEBOOK"));
String INSTAGRAM = movie.setINSTAGRAM(obj.getString("INSTAGRAM"));
String TWITTER = movie.setTWITTER(obj.getString("TWITTER"));
String WEBSITE = movie.setWEBSITE(obj.getString("WEBSITE"));
if(arg0.getTitle().equals(Name)){
Intent intent = new Intent(Newtab.this, SingleItemView.class);
intent.putExtra("NAME",Name);
// Start SingleItemView Class
intent.putExtra("DESCRIPTION", DESCRIPTION);
intent.putExtra("EMAILS", EMAILS);
intent.putExtra("ADDRESS", ADDRESS);
intent.putExtra("IMAGELINK", IMAGELINK);
intent.putExtra("PHONES", PHONES);
intent.putExtra("LATITUDE", LATITUDE);
intent.putExtra("LONGITUDE", LONGITUDE);
intent.putExtra("FACEBOOK", FACEBOOK);
intent.putExtra("INSTAGRAM", INSTAGRAM);
intent.putExtra("TWITTER", TWITTER);
intent.putExtra("WEBSITE", WEBSITE);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return false;
}
});
Bitmap bmImg = Ion.with(getBaseContext())
.load("http://app-chef.com/SquareCard/App/Categories_icons/"+cate+".png").asBitmap().get();
marker.icon(BitmapDescriptorFactory.fromBitmap(bmImg));
// adding marker
mMap.addMarker(marker);
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.setMyLocationEnabled(true);
if (Global.able==1){
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double mylong = location.getLongitude();
double mylat = location.getLatitude();
LatLng coordinate = new LatLng(mylat, mylong);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 15);
mMap.animateCamera(yourLocation);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
final ActionBar actionBar = getActionBar();
//actionBar.setDisplayShowTitleEnabled(true);
//actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(Html.fromHtml("<font color='#ffffff'>Back </font>"));
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2d76ba")));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}
private void showGPSDisabledAlertToUser(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Please Enable your GPS")
.setCancelable(false)
.setNegativeButton("Goto Settings Page To Enable GPS",
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("Cancel",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap != null) {
return;
}
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap == null) {
return;
}
// Initialize map options. For example:
// mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
答案 0 :(得分:0)
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);`
double mylong = location.getLongitude();
double mylat = location.getLatitude();
永远不要假设location
不为空,当用户没有GPS锁定或禁用时,它可以为空