我不知道我的代码有什么问题,我花了半天时间在谷歌周围搜索来解决这个问题,但是没有运气,我已经导入了谷歌库,但仍然没有运气,我的安卓应用程序仍然不幸停止了map = fragment.getMap();
在里面使用SherlockFragment
,如果不使用getMap()
地图会很好地显示,但是当我调用它时,地图停止了,这是我的代码,希望有办法解决这个问题
import com.actionbarsherlock.app.SherlockFragment;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class PetaActivity extends SherlockFragment {
private SupportMapFragment fragment;
private GoogleMap map;
// -6.240344, 106.838422
private final LatLng SME_TOWER = new LatLng(-6.234813, 106.826731);
private final LatLng SME_TOWER_1 = new LatLng(-6.236637, 106.824671);
private final LatLng SME_TOWER_2 = new LatLng(-6.237938, 106.827031);
private final LatLng SME_TOWER_3 = new LatLng(-6.232040, 106.826334);
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
android.support.v4.app.FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, fragment).commit();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.activity_peta, container,
false);
return rootView;
}
@Override
public void onResume() {
super.onResume();
LocationManager locationManager = (LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE);
boolean lokasi = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!lokasi) {
AlertDialog.Builder alertDlg = new AlertDialog.Builder(
getActivity());
alertDlg.setMessage("Pendeteksian Lokasi Tidak Aktif");
alertDlg.setCancelable(false);
alertDlg.setPositiveButton("Cek Setting",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
alertDlg.create().show();
} else {
if (map == null) {
map = fragment.getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.setMyLocationEnabled(true);
map.setInfoWindowAdapter(new MyInfoWindowAdapter());
LocationManager service = (LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = service.getBestProvider(criteria, false);
Location location = service.getLastKnownLocation(provider);
@SuppressWarnings("unused")
LatLng userLocation = new LatLng(location.getLatitude(),
location.getLongitude());
// map.moveCamera(
// CameraUpdateFactory.newLatLngZoom(userLocation,
// 14.0f));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(SME_TOWER,
14.0f));
// map.addMarker(new
// MarkerOptions().position(userLocation)).setTitle("Halo, PILAR CIPTA SOLUSI");
Marker tampil = map.addMarker(new MarkerOptions()
.position(SME_TOWER)
.title("Puri Denpasar Hotel Jakarta")
.snippet("Kuningan Kota Jakarta Selatan")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
tampil.showInfoWindow();
map.addMarker(new MarkerOptions()
.position(SME_TOWER_1)
.title("Pelaksanaan Peremajaan Pengurus RT dan RW di Kelurahan Kuningan Barat")
.snippet("Nilai Anggaran Rp. 20,170,000")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
map.addMarker(new MarkerOptions()
.position(SME_TOWER_2)
.title("Penyediaan Jasa TALI Kantor Kelurahan Kuningan Barat")
.snippet("Nilai Anggaran Rp. 146,000,000")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
map.addMarker(new MarkerOptions()
.position(SME_TOWER_3)
.title("Pelaksaan Kegiatan Posyandu dan Pemberian Makanan Tambahan (PMT)")
.snippet("Nilai Anggaran Rp.184,000,000")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}
}
}
class MyInfoWindowAdapter implements InfoWindowAdapter {
private final View myContentsView;
MyInfoWindowAdapter() {
myContentsView = getActivity().getLayoutInflater().inflate(
R.layout.custom_info_contents, null);
}
@Override
public View getInfoContents(Marker marker) {
TextView tvTitle = ((TextView) myContentsView
.findViewById(R.id.title));
tvTitle.setText(marker.getTitle());
TextView tvSnippet = ((TextView) myContentsView
.findViewById(R.id.snippet));
tvSnippet.setText(marker.getSnippet());
return myContentsView;
}
@Override
public View getInfoWindow(Marker marker) {
// TODO Auto-generated method stub
return null;
}
}
}
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Lots of fancy layout -->
<RelativeLayout
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</RelativeLayout>
here is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="appinventor.ai_wenkhairu.Pantura"
android:versionCode="1"
android:versionName="1.2" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="18" />
<permission
android:name="appinventor.ai_wenkhairu.Pantura.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="your_package_name.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light" >
<activity
android:name=".SplashScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:label="@string/app_name" />
<activity
android:name=".FotoActivity"
android:label="@string/title_activity_foto" />
<activity
android:name=".PetaActivity"
android:label="@string/title_activity_peta" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAsgga.................." />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
更新,这是我的 logcat
08-26 02:23:54.862: D/dalvikvm(16199): GC_FOR_ALLOC freed 58K, 8% free 2457K/2648K, paused 5ms, total 6ms
08-26 02:23:55.006: I/dalvikvm-heap(16199): Grow heap (frag case) to 9.102MB for 6898332-byte allocation
08-26 02:23:55.018: D/dalvikvm(16199): GC_FOR_ALLOC freed <1K, 3% free 9193K/9388K, paused 7ms, total 7ms
08-26 02:23:55.026: D/dalvikvm(16199): GC_CONCURRENT freed 0K, 3% free 9193K/9388K, paused 2ms+4ms, total 7ms
08-26 02:23:55.142: D/libEGL(16199): loaded /system/lib/egl/libEGL_emulation.so
08-26 02:23:55.142: D/(16199): HostConnection::get() New Host Connection established 0xb8037100, tid 16199
08-26 02:23:55.162: D/libEGL(16199): loaded /system/lib/egl/libGLESv1_CM_emulation.so
08-26 02:23:55.166: D/libEGL(16199): loaded /system/lib/egl/libGLESv2_emulation.so
08-26 02:23:55.266: W/EGL_emulation(16199): eglSurfaceAttrib not implemented
08-26 02:23:55.290: D/OpenGLRenderer(16199): Enabling debug mode 0
08-26 02:23:57.366: D/dalvikvm(16199): GC_FOR_ALLOC freed 233K, 4% free 9960K/10328K, paused 6ms, total 6ms
08-26 02:23:57.394: D/dalvikvm(16199): GC_FOR_ALLOC freed 2K, 4% free 11059K/11432K, paused 4ms, total 4ms
08-26 02:23:57.442: I/dalvikvm-heap(16199): Grow heap (frag case) to 13.342MB for 2536932-byte allocation
08-26 02:23:57.454: D/dalvikvm(16199): GC_FOR_ALLOC freed <1K, 3% free 13537K/13912K, paused 10ms, total 10ms
08-26 02:23:57.462: D/dalvikvm(16199): GC_CONCURRENT freed 0K, 3% free 13537K/13912K, paused 4ms+0ms, total 10ms
08-26 02:23:57.646: W/EGL_emulation(16199): eglSurfaceAttrib not implemented
08-26 02:23:57.702: W/EGL_emulation(16199): eglSurfaceAttrib not implemented
08-26 02:23:59.490: D/TilesManager(16199): Starting TG #0, 0xb8299a78
08-26 02:23:59.510: D/TilesManager(16199): new EGLContext from framework: b801e3f0
08-26 02:23:59.510: D/GLWebViewState(16199): Reinit shader
08-26 02:23:59.754: D/GLWebViewState(16199): Reinit transferQueue