百度地图开发中的java.lang.ClassCastException解决方法及定位源码

在开发百度地图定位功能时遇到`java.lang.ClassCastException`,问题根源在于AndroidManifest.xml中Application配置错误。解决方案是在Application节点下指定自定义的application类,如`android:name="com.example.maplocation.LocationApplication"`。此外,文章还提供了百度地图定位的核心代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


   在近来开发百度地图定位功能时有出现了java.lang.ClassCastException: android.app.Application cannot be cast to com.example.maplocation.LocationApplication问题,其解决方案为在manifest.xml文件中进行配置。在Application节点下添加android:name="自己写的application"。在本代码中就应该写:

<application
        android:name="com.example.maplocation.LocationApplication">

另将百度地图定位功能核心代码贴出

public class LocationActivity extends Activity{
	private LocationClient mLocationClient;
	private TextView LocationResult,ModeInfor;
	private Button startLocation;
	private RadioGroup selectMode,selectCoordinates;
	private EditText frequence;
	private LocationMode tempMode = LocationMode.Hight_Accuracy;
	private String tempcoor="gcj02";
	private CheckBox checkGeoLocation;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location_main);
		mLocationClient = ((LocationApplication)getApplication()).mLocationClient;
		//此处要注意,如果在mainest.xml中不进行application声明
		//就会抛出异常,classcastexception
		LocationResult = (TextView)findViewById(R.id.tv_result);
		ModeInfor= (TextView)findViewById(R.id.modeinfor);
		ModeInfor.setText(getString(R.string.hight_accuracy_desc));
		 ((LocationApplication)getApplication()).mLocationResult = LocationResult;
		 frequence = (EditText)findViewById(R.id.frequence);
		 checkGeoLocation = (CheckBox)findViewById(R.id.geolocation);
		startLocation = (Button)findViewById(R.id.addfence);
		startLocation.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				InitLocation();
				
				if(startLocation.getText().equals(getString(R.string.startlocation))){
					mLocationClient.start();
					startLocation.setText(getString(R.string.stoplocation));
				}else{
					mLocationClient.stop();
					startLocation.setText(getString(R.string.startlocation));
				}
				
				
			}
		});
		selectMode = (RadioGroup)findViewById(R.id.selectMode);
		selectCoordinates= (RadioGroup)findViewById(R.id.selectCoordinates);
		selectMode.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				String ModeInformation = null;
				switch (checkedId) {
				case R.id.radio_hight:
					tempMode = LocationMode.Hight_Accuracy;
					ModeInformation = getString(R.string.hight_accuracy_desc);
					break;
				case R.id.radio_low:
					tempMode = LocationMode.Battery_Saving;
					ModeInformation = getString(R.string.saving_battery_desc);
					break;
				case R.id.radio_device:
					tempMode = LocationMode.Device_Sensors;
					ModeInformation = getString(R.string.device_sensor_desc);
					break;
				default:
					break;
				}
				ModeInfor.setText(ModeInformation);
			}
		});
		selectCoordinates.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.radio_gcj02:
					tempcoor="gcj02";
					break;
				case R.id.radio_bd09ll:
					tempcoor="bd09ll";
					break;
				case R.id.radio_bd09:
					tempcoor="bd09";
					break;
				default:
					break;
				}
			}
		});
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		mLocationClient.stop();
		super.onStop();
	}

	private void InitLocation(){
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(tempMode);//设置定位模式
		option.setCoorType(tempcoor);//返回的定位结果是百度经纬度,默认值gcj02
		int span=1000;
		//设置刷新时间频率
		try {
			span = Integer.valueOf(frequence.getText().toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		option.setScanSpan(span);//设置发起定位请求的间隔时间为5000ms
		option.setIsNeedAddress(checkGeoLocation.isChecked());
		mLocationClient.setLocOption(option);
	}
package com.example.maplocation;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.GeofenceClient;
import com.baidu.location.LocationClient;
import android.app.Application;
import android.app.Service;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;

/**
 * 主Application
 */
public class LocationApplication extends Application {
	public LocationClient mLocationClient;
	public GeofenceClient mGeofenceClient;
	public MyLocationListener mMyLocationListener;
	
	public TextView mLocationResult,logMsg;
	public TextView trigger,exit;
	public Vibrator mVibrator;
	
	@Override
	public void onCreate() {
		super.onCreate();
		mLocationClient = new LocationClient(this.getApplicationContext());
		mMyLocationListener = new MyLocationListener();
		mLocationClient.registerLocationListener(mMyLocationListener);
	}

	
	/**
	 * 实现实位回调监听
	 */
	public class MyLocationListener implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			//Receive Location 
			StringBuffer sb = new StringBuffer(256);
			sb.append("time : ");
			sb.append(location.getTime());
			sb.append("\nerror code : ");
			sb.append(location.getLocType());
			sb.append("\nlatitude : ");
			sb.append(location.getLatitude());
			sb.append("\nlontitude : ");
			sb.append(location.getLongitude());
			sb.append("\nradius : ");
			sb.append(location.getRadius());
			logMsg(sb.toString());
			Log.i("BaiduLocationApiDem", sb.toString());
		}


	}
	
	
	/**
	 * 显示请求字符串
	 * @param str
	 */
	public void logMsg(String str) {
		try {
			if (mLocationResult != null)
				mLocationResult.setText(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 高精度地理围栏回调
	 * @author jpren
	 *
	 */
	
}
其中页面布局和主MainActivity类很简单就不再贴出,如果想要详细的研究百度地图最好还是看百度地图api 点击打开链接。并且在开发时一定要注意key的配置、相关权限和application的配置。





引用\[1\]中提到,这个错误是由于在使用datax-common中的Configuration.java工具类时,当设置的json中的值字符串内容也包含\[\]时,调用Object object = this.get(path, List.class);返回的内容为String,而不是List对象,导致类型转换异常。为了修复这个问题,可以对代码进行如下修复:在getList方法中添加异常处理,当发生类型转换异常时,将String值添加到List中。然后重新打包datax-common模块,将datax/lib下的datax-common-0.0.1-SNAPSHOT.jar替换为新打好的jar。\[1\] 引用\[2\]中提到,还可以修改属性类型,将List<Operation>修改为内部类OperationList,以解决泛型擦除问题。\[2\] 引用\[3\]中提到,还需要在相应的配置文件中添加javaType="org.example.modules.business.entity.SysMenu$OperationList",以确保正确的类型处理。\[3\] 综上所述,要解决DataX引擎运行过程中的java.lang.ClassCastException错误,可以根据上述修复方法进行操作。 #### 引用[.reference_title] - *1* [DataX HdfsReader 源码分析,及空文件 Bug修复和路径正则功能增强](https://blog.csdn.net/github_39577257/article/details/106276811)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [数据库json格式字段映射,以及泛型擦除笔记](https://blog.csdn.net/rakunjo/article/details/123066912)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值