2 zk930502 zk930502 于 2016.02.25 11:16 提问

【新手求助】安卓 利用百度地图定位 5C

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

求帮助解决错误
想知道怎么能定位出当前城市

7个回答

mengyin521
mengyin521   2016.02.25 11:19

直接按SDK 中的 DEMO 来阿!

luochoudan
luochoudan   Ds   Rxr 2016.02.25 11:22

com.zk.dddd.MainActivity,类没有找到,clean一下试试,建议你先把demo跑一下,看懂,然后再修改不耽误功夫。

qq_27665781
qq_27665781   Rxr 2016.02.25 11:46

直接把demo复制过来算了

VNanyesheshou
VNanyesheshou   2016.02.25 13:09

MainActivity没有在清单文件中配置,你看看AndroidManifest.xml文件。
百度demo http://blog.csdn.net/vnanyesheshou/article/details/49924237

Acmen99
Acmen99   2016.02.25 19:54

和demo对比一下,看看是不是少了什么?多研究研究demo!

Leo1208
Leo1208   2016.02.25 23:34

你看下你的activity有没有在清单文件上注册啊

bao970583267
bao970583267   2016.02.26 17:19

去百度开发平台看文档,一步一步来就出来了

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!