• 解决问题 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo


    问题:

    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.express_delivery/com.example.express_delivery.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.express_delivery.MainActivity> has no zero argument constructor

    并且软件进入后直接闪屏退出

    其错误图:

    解决:

    我上网搜了很多,他们的问题大都和我的不一样

    参考博客:

    https://blog.csdn.net/dukongjian/article/details/87385491

    https://www.cnblogs.com/z1141000271/p/7905912.html

    https://blog.csdn.net/disanxiaoyao/article/details/80047737?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

    https://blog.csdn.net/cshichao/article/details/9979621?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param

    其他的我就不写了,因为我看过之后,问题前半部分一样,但是后面不一样了就,(当然我也按着他们的来了一下,还是会出问题)于是我就接着检查和官方给的步骤,哪不一样,

    后来发现当时写的时候,有一点是不太明白

    这句话中的mBaiduMap是怎么来的,我就又上网搜了搜(还看了看以前搜的百度地图定位的案例代码),后来终于找到了

    1.首先是需要在代码的开始,定义一下

    2.然后获取地图层

    然后才能使用他,完成我刚才不确定的那段代码

    //开启地图的定位图层
    mBaiduMap.setMyLocationEnabled(true);

    完整的MainActivity.java

    package com.example.express_delivery;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.companion.WifiDeviceFilter;
    import android.os.Bundle;
    
    import com.baidu.location.BDAbstractLocationListener;
    import com.baidu.location.BDLocation;
    import com.baidu.location.LocationClient;
    import com.baidu.location.LocationClientOption;
    import com.baidu.mapapi.SDKInitializer;
    import com.baidu.mapapi.map.BaiduMap;
    import com.baidu.mapapi.map.MapStatusUpdate;
    import com.baidu.mapapi.map.MapStatusUpdateFactory;
    import com.baidu.mapapi.map.MapView;
    import com.baidu.mapapi.map.MyLocationData;
    import com.baidu.mapapi.model.LatLng;
    
    public class MainActivity extends AppCompatActivity {
    
        private MapView mMapView = null;
    
        private BaiduMap mBaiduMap;
        private LocationClient mLocationClient;
        private MyLocationListener mLocationListener;
        //是否是第一次定位,如果是第一次定位的话要将自己的位置显示在地图的中间
        private boolean isFirstLocation = true;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //在使用SDK各组件之前初始化context信息,传入ApplicationContext
            //注意该方法要在setContextView 方法之前实现
            SDKInitializer.initialize(getApplicationContext());
            setContentView(R.layout.activity_main);
    
            //获取地图控件引用
            mMapView = findViewById(R.id.bmapView);
            mBaiduMap = mMapView.getMap();
            //开启地图的定位图层
            mBaiduMap.setMyLocationEnabled(true);
            //initLocation();
    
            //定位初始化
            mLocationClient = new LocationClient(this);
    
            //通过LocationClientOption设置LocationClient相关参数
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true); // 打开gps
            option.setCoorType("bd09ll"); // 设置坐标类型
            option.setScanSpan(1000);
    
            //设置locationClientOption
            mLocationClient.setLocOption(option);
    
            //注册LocationListener监听器
            MyLocationListener myLocationListener = new MyLocationListener();
            mLocationClient.registerLocationListener(myLocationListener);
            //开启地图定位图层
            mLocationClient.start();
    
        }
    
        private void initLocation() {
            //定位初始化
            mLocationClient = new LocationClient(this);
    
            //通过LocationClientOption设置LocationClient相关参数
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true); // 打开gps
            option.setCoorType("bd09ll"); // 设置坐标类型
            option.setScanSpan(1000);
    
            //设置locationClientOption
            mLocationClient.setLocOption(option);
    
            //注册LocationListener监听器
            MyLocationListener myLocationListener = new MyLocationListener();
            mLocationClient.registerLocationListener(myLocationListener);
            //开启地图定位图层
            mLocationClient.start();
        }
    
    
        public class MyLocationListener extends BDAbstractLocationListener{
            @Override
            public void onReceiveLocation(BDLocation location){
                //mapView 销毁后不再处理新接收的位置
                if(location == null || mMapView == null){
                    return;
                }
                MyLocationData locData = new MyLocationData.Builder()
                        .accuracy(location.getRadius())
                        // 此处设置开发者获取到的方向信息,顺时针0-360
                        .direction(location.getDirection()).latitude(location.getLatitude())
                        .longitude(location.getLongitude()).build();
                mBaiduMap.setMyLocationData(locData);
    
                if(isFirstLocation){
                    //获取经纬度
                    LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
                    MapStatusUpdate status = MapStatusUpdateFactory.newLatLng(ll);
                    //mBaiduMap.setMapStatus(status);//直接到中间
                    mBaiduMap.animateMapStatus(status);//动画的方式到中间
                    isFirstLocation = false;
    
                }
            }
        }
    
    
        @Override
        protected void onResume(){
            super.onResume();
            //在activity执行onResume 时,执行mMapView.onResume(),实现地图生命周期管理
            mMapView.onResume();
    
        }
    
        @Override
        protected void onPause(){
            super.onPause();
            //在activity执行onPause 时,执行mMapView.onPause(),实现地图生命周期管理
            mMapView.onPause();
    
        }
    
        @Override
        protected void onDestroy(){
            super.onDestroy();
            mLocationClient.stop();
            mBaiduMap.setMyLocationEnabled(false);
            //在activity执行onDestroy 时,执行mMapView.onDestroy(),实现地图生命周期管理
            mMapView.onDestroy();
            mMapView = null;
    
        }
    }
    View Code
  • 相关阅读:
    System.Runtime.InteropServices.COMException (0x800706BA) 解决方法
    Win7 不能安装 msi 解决办法
    note 1 对象和数据类型
    note 0 Python介绍及Python IDE环境安装 Spyder with Anaconda
    无法获得锁 /var/lib/dpkg/lock
    keil5 MDK 链接报错 Error: L6410W 解决
    手动卸载CAD 删除残留文件 清理遗留的文件
    keil5 MDK warning:registered ARM compiler version not found in path
    干掉hao123劫持浏览器主页
    Win7 无法访问Installer服务
  • 原文地址:https://www.cnblogs.com/022414ls/p/13442285.html
Copyright © 2020-2023  润新知