Google 地图应用程序 - 运行应用程序时出错

Posted

技术标签:

【中文标题】Google 地图应用程序 - 运行应用程序时出错【英文标题】:Google Maps Application - Errors Running App 【发布时间】:2020-04-29 12:52:33 【问题描述】:

我目前正在开发一个获取用户位置的应用程序,并允许用户使用 places API 查看附近的位置。每当我尝试运行代码时,我都会收到多个错误,这些错误可以追溯到我的主 MapActivity 类中的一个错误,这就是错误的外观:

 at projektapplikation.MapsActivity.onCreate(MapsActivity.java:60)

获取错误的方法称为 OnCreate,如下所示:

@Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        //Init Service
        mServiceType = Common.getGoogleAPIServiceType();

        //Request Runtime Permission
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
            checkLocationPermission();
        

        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() 
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) 

                switch (item.getItemId()) 
                    case R.id.action_hospital:
                        nearByPlace("Sjukhus");
                        break;
                    case R.id.action_market:
                        nearByPlace("Shopping");
                        break;
                    case R.id.action_retaurant:
                        nearByPlace("Resturang");
                        break;
                    case R.id.action_school:
                        nearByPlace("Skola");
                        break;
                    default:
                        break;

                


                return true;
            
        );
    

这就是我的 gradle 的样子:

apply plugin: 'com.android.application'

android 
    compileSdkVersion 28
    defaultConfig 
        applicationId "se.hig.p18wisa02.projektapplikation"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    


    repositories
            
                jcenter()
                google()

            

    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        
    


allprojects 
    repositories 
        google()
        jcenter()
    


dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.squareup.retrofit2:retrofit:2.7.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    compile 'com.android.support:design:28.0.0'
    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    implementation 'com.google.android.gms:play-services:16.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

这些都是我的错误:

    2020-01-12 18:37:08.508 26780-26780/se.hig.p18wisa02.projektapplikation E/jektapplikatio: The String#value field is not present on Android versions >= 6.0
2020-01-12 18:37:08.806 26780-26780/se.hig.p18wisa02.projektapplikation D/AndroidRuntime: Shutting down VM
2020-01-12 18:37:08.807 26780-26780/se.hig.p18wisa02.projektapplikation E/AndroidRuntime: FATAL EXCEPTION: main
    Process: se.hig.p18wisa02.projektapplikation, PID: 26780
    java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method
        at okhttp3.internal.Util.<clinit>(Util.java:87)
        at okhttp3.internal.Util.skipLeadingAsciiWhitespace(Util.java:321)
        at okhttp3.HttpUrl$Builder.parse(HttpUrl.java:1313)
        at okhttp3.HttpUrl.get(HttpUrl.java:917)
        at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:492)
        at se.hig.p18wisa02.projektapplikation.Remote.RetrofitClient.getClient(RetrofitClient.java:10)
        at se.hig.p18wisa02.projektapplikation.Common.getGoogleAPIServiceType(Common.java:12)
        at se.hig.p18wisa02.projektapplikation.MapsActivity.onCreate(MapsActivity.java:67)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.ClassCastException: Bootstrap method returned null
        at okhttp3.internal.Util.<clinit>(Util.java:87) 
        at okhttp3.internal.Util.skipLeadingAsciiWhitespace(Util.java:321) 
        at okhttp3.HttpUrl$Builder.parse(HttpUrl.java:1313) 
        at okhttp3.HttpUrl.get(HttpUrl.java:917) 
        at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:492) 
        at se.hig.p18wisa02.projektapplikation.Remote.RetrofitClient.getClient(RetrofitClient.java:10) 
        at se.hig.p18wisa02.projektapplikation.Common.getGoogleAPIServiceType(Common.java:12) 
        at se.hig.p18wisa02.projektapplikation.MapsActivity.onCreate(MapsActivity.java:67) 
        at android.app.Activity.performCreate(Activity.java:7136) 
        at android.app.Activity.performCreate(Activity.java:7127) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

我不知道需要进行哪些更改才能使其正常工作,因此非常感谢您的帮助。

【问题讨论】:

【参考方案1】:

最新发布的com.google.android.gms:play-services 版本是12.0.1 宣布here,所以改变

implementation 'com.google.android.gms:play-services:16.0.0'

implementation 'com.google.android.gms:play-services:12.0.1'

另外请注意,您在使用 AndroidX 时不应使用旧版支持存储库。所以不是

compile'com.android.support:design:28.0.0'

使用

implementation 'com.google.android.material:material:1.0.0'

【讨论】:

以上是关于Google 地图应用程序 - 运行应用程序时出错的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 9 上运行时确实会出现 Google 地图图块

尝试使用 Google 地图预加载图标时出错 - INVALID_STATE_ERR: DOM Exception 11

在 Google 地图导航正在运行时请求位置更新

Android Studio:膨胀类片段时出错

使用 google toolbox for mac 编译单元测试时出错

即时运行和谷歌地图的问题