问题描述
- 【新手求助】安卓 利用百度地图定位
-
import java.util.List;import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity {
private Button bt; private TextView tv_coordinate; private TextView tv_city; private String provider; private LocationManager locationManager; private LocationClient locationClient = null; private LocationClientOption option;
// private BDLocationListener bdLocationListener;
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); tv_coordinate = (TextView)findViewById(R.id.textView1); tv_city = (TextView)findViewById(R.id.textView2); bt = (Button)findViewById(R.id.button1); locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); bt.setOnClickListener(new OnClickListener() { public void onClick(View v) { } }); //坐标 List<String> providerList = locationManager.getProviders(true); if(providerList.contains(LocationManager.GPS_PROVIDER)) { provider = LocationManager.GPS_PROVIDER; }else if(providerList.contains(LocationManager.NETWORK_PROVIDER)) { provider = LocationManager.NETWORK_PROVIDER; }else { Toast.makeText(MainActivity.this, "No Location provider", Toast.LENGTH_SHORT).show(); return; } Location location = locationManager.getLastKnownLocation(provider); if(location != null) { showLocation(location); } locationManager.requestLocationUpdates(provider, 5000, 10, locationListener); //城市名 locationClient = new LocationClient(getApplicationContext()); locationClient.registerLocationListener(bdLocationListener); option.setCoorType("all"); option.setOpenGps(true); BDLocation bdLocation = locationClient.getLastKnownLocation(); if(bdLocation != null) { showBDlocation(bdLocation); } locationClient.registerNotifyLocationListener(bdLocationListener); locationClient.requestLocation(); locationClient.requestNotifyLocation(); } protected void onDestroy() { super.onDestroy(); if(locationManager != null) { locationManager.removeUpdates(locationListener); } } LocationListener locationListener = new LocationListener() { public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } public void onLocationChanged(Location location) { showLocation(location); } }; public void showLocation(Location location) { String coordinate = "纬度:" + location.getLatitude() + "n" + "经度:" + location.getLongitude(); tv_coordinate.setText(coordinate); } BDLocationListener bdLocationListener = new BDLocationListener() { public void onReceiveLocation(BDLocation bdlocation) { showBDlocation(bdlocation); } }; public void showBDlocation(BDLocation bdLocation) { String city = bdLocation.getCity(); tv_city.setText(city); }
}
运行报错java.lang.NoClassDefFoundError: com.zk.dddd.MainActivity$2
求帮助解决错误
想知道怎么能定位出当前城市
解决方案
直接按SDK 中的 DEMO 来阿!
解决方案二:
com.zk.dddd.MainActivity,类没有找到,clean一下试试,建议你先把demo跑一下,看懂,然后再修改不耽误功夫。
解决方案三:
直接把demo复制过来算了
解决方案四:
MainActivity没有在清单文件中配置,你看看AndroidManifest.xml文件。
百度demo http://blog.csdn.net/vnanyesheshou/article/details/49924237
解决方案五:
和demo对比一下,看看是不是少了什么?多研究研究demo!
解决方案六:
你看下你的activity有没有在清单文件上注册啊
解决方案七:
去百度开发平台看文档,一步一步来就出来了