不好意思,我也是按照老師說(shuō)的打的,為什么閃退,能不能幫忙看看哪里出錯(cuò)了,非常感謝
public class MainActivity extends Activity {
MapView mMapView = null;
private BaiduMap mBaiduMap;
//定位相關(guān)
private LocationClient mLocationClient;
private MyLocationListener mLocationListener;
private boolean isFirstIn = true;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState)?
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//在使用SDK各組件之前初始化context信息,傳入ApplicationContext ?
? ? ? ? //注意該方法要再setContentView方法之前實(shí)現(xiàn) ?
? ? ? ? SDKInitializer.initialize(getApplicationContext());//添加包
setContentView(R.layout.activity_main);
this.context = this;
initView();
initLocation();
}
private void initLocation() {
mLocationClient = new LocationClient(this);
mLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mLocationListener);
LocationClientOption option = new LocationClientOption();
option.setCoorType("bd09ll");
option.setIsNeedAddress(true);
option.setOpenGps(true);
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
}
private void initView() {
mMapView=(MapView)findViewById(R.id.id_bmapView);
mBaiduMap = mMapView.getMap();
MapStatusUpdate msu=MapStatusUpdateFactory.zoomTo(15.0f);//放大至500米
mBaiduMap.setMapStatus(msu);
}
@Override ?
? ? protected void onDestroy() { ?
? ? ? ? super.onDestroy(); ?
? ? ? ? //在activity執(zhí)行onDestroy時(shí)執(zhí)行mMapView.onDestroy(),實(shí)現(xiàn)地圖生命周期管理 ?
? ? ? ? mMapView.onDestroy(); ?
? ? } ?
? ? @Override ?
? ? protected void onResume() { ?
? ? ? ? super.onResume(); ?
? ? ? ? //在activity執(zhí)行onResume時(shí)執(zhí)行mMapView. onResume (),實(shí)現(xiàn)地圖生命周期管理 ?
? ? ? ? mMapView.onResume(); ?
? ? ? ? } ?
? ? protected void onStart()
{
super.onStart();
// 開(kāi)啟定位
mBaiduMap.setMyLocationEnabled(true);
if (!mLocationClient.isStarted())
mLocationClient.start();
}
? ? @Override ?
? ? protected void onPause() { ?
? ? ? ? super.onPause(); ?
? ? ? ? //在activity執(zhí)行onPause時(shí)執(zhí)行mMapView. onPause (),實(shí)現(xiàn)地圖生命周期管理 ?
? ? ? ? mMapView.onPause(); ?
? ? ? ? } ?
? ? protected void onStop()
{
super.onStop();
// 停止定位
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.id_map_common:
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
break;
case R.id.id_map_site:
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
break;
case R.id.id_map_traffic:
if (mBaiduMap.isTrafficEnabled())
{
mBaiduMap.setTrafficEnabled(false);
item.setTitle("實(shí)時(shí)交通(off)");
} else
{
mBaiduMap.setTrafficEnabled(true);
item.setTitle("實(shí)時(shí)交通(on)");
}
? ? ? ? ?}
return super.onOptionsItemSelected(item);
}
private class MyLocationListener implements BDLocationListener
{
@Override
public void onReceiveLocation(BDLocation location) {
MyLocationData data = new MyLocationData.Builder()//
.accuracy(location.getRadius())//
.latitude(location.getLatitude())//
.longitude(location.getLongitude())//
.build();
mBaiduMap.setMyLocationData(data);
if (isFirstIn)
{
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
isFirstIn = false;
Toast.makeText(context, location.getAddrStr(),Toast.LENGTH_SHORT).show();
}
}
}
}
2016-04-27
2016-03-29
你的項(xiàng)目是什么版本的,我的是Android4.4,我的一直都沒(méi)有問(wèn)題
2016-03-29
你的menu文件夾里的布局文件寫(xiě)了吧?你是從哪一步開(kāi)始閃退的,閃退的原因有很多,權(quán)限什么的,可以在運(yùn)行的時(shí)候看一下logcat信息