Android中自定义Application报ClassCastException错误

来源:互联网 发布:java 代理模式详解 编辑:程序博客网 时间:2024/05/22 17:35

       近日在做一个百度的地图定位项目,自定义了一个Application类,在Activity中调用时报ClassCastException错误,对此搜了一些资料,开始以为是逻辑错误,搜了一些资料后,发现解决问题的方法很简单,就是在AndroidManifest中配置相关信息即可。

       下面来看看Application:在Android中,有一个名为Application的类,我们可以在Activity中应用getApplication()方法来获得,它是代表我们的应用方法的类,应用它可以获合适前应用的主题,资料文件中的内容等,这个类更灵活的一个特点就是可以被我们持续,来添加我们本身的全局属性

       例如我的百度定位应用,要显示定位信息,那么我们就要持续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);
        mGeofenceClient = new GeofenceClient(getApplicationContext());
        
        
        mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
    }

    
    /**
     * 显示定位结果
     */
    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());
            if (location.getLocType() == BDLocation.TypeGpsLocation){
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
                sb.append("\ndirection : ");
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
                sb.append(location.getDirection());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
                //地址名称
                sb.append("\noperationers : ");
                sb.append(location.getOperators());
            }
            logMsg(sb.toString());
            Log.i("BaiduLocationApiDem", sb.toString());
        }

    }
    
   
    public void logMsg(String str) {
        try {
            if (mLocationResult != null)
                mLocationResult.setText(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}

       之后,最关键的就是在AndroidManifest中将我们扩大的Application添加上去,即添加你的Application名称,如下:

<application
        android:name="com.gwi.hiden.ui.LocationApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

       如此定义好之后,我们的自定义Application就算完成了,然后我们可以很便利的在任何Activity和View中来获取定位属性了,代码如下:

        mLocationClient = ((LocationApplication) getApplication()).mLocationClient;


        OK!问题解决!

0 0
原创粉丝点击