MapBox onCreate 实例在启动时崩溃 android Java

MapBox onCreate Instance Crashed onStart android Java

所以我在 Android Java.

中创建了一个 MapBox 实例

我使用的版本:

'com.mapbox.mapboxsdk:mapbox-android-sdk:9.0.0'

这是我的以下代码:

AndroidManifest.xml

...
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" />
    ...

activity_main.xml

   <com.mapbox.mapboxsdk.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

Application.java:

public class Application extends AppCompatActivity implements LocationListener
{
   ....

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        /* Initialise Mapbox Services */
        Mapbox.getInstance(this, this.getResources().getString(R.string.mapbox_token));

        /* Set Layout Content View  */
        Application.this.setContentView(R.layout.activity_main);

        /* Set MapView to Instances of the current activity */
        Application.this.mapView = (MapView) this.findViewById(R.id.mapview);
        Application.this.mapView.onCreate(savedInstanceState); // THIS CAUSE ERROR
    }
    ...
    ... // Some code here

}

怀疑第 Application.this.mapView.onCreate(savedInstanceState); 行是导致错误的原因。

我试着用原始的方式来做,(意思是不遵循公司编码指南)。异常仍然存在。

我得到的异常:


2022-01-21 23:04:48.816 21806-21806/my.com.johnmelody.emergenciesresponsedemo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: my.com.johnmelody.emergenciesresponsedemo, PID: 21806
    java.lang.RuntimeException: Unable to start activity ComponentInfo{my.com.johnmelody.emergenciesresponsedemo/my.com.johnmelody.emergenciesresponsedemo.Application}: java.lang.SecurityException: getDataNetworkTypeForSubscriber
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3479)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3651)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2104)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:7861)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
     Caused by: java.lang.SecurityException: getDataNetworkTypeForSubscriber
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2376)
        at android.os.Parcel.createException(Parcel.java:2360)
        at android.os.Parcel.readException(Parcel.java:2343)
        at android.os.Parcel.readException(Parcel.java:2285)
        at com.android.internal.telephony.ITelephony$Stub$Proxy.getNetworkTypeForSubscriber(ITelephony.java:8764)
        at android.telephony.TelephonyManager.getNetworkType(TelephonyManager.java:3034)
        at android.telephony.TelephonyManager.getNetworkType(TelephonyManager.java:2998)
        at com.mapbox.android.telemetry.TelemetryUtils.obtainCellularNetworkType(TelemetryUtils.java:165)
        at com.mapbox.mapboxsdk.module.telemetry.PhoneState.<init>(PhoneState.java:39)
        at com.mapbox.mapboxsdk.module.telemetry.TelemetryImpl.onAppUserTurnstileEvent(TelemetryImpl.java:46)
        at com.mapbox.mapboxsdk.maps.MapView.onCreate(MapView.java:303)
        at my.com.johnmelody.emergenciesresponsedemo.Application.onCreate(Application.java:100)
        at android.app.Activity.performCreate(Activity.java:8109)
        at android.app.Activity.performCreate(Activity.java:8083)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3452)
    ```

Any idea, what I do wrong here? 

已解决。

已请求 READ_PHONE_STATE 许可解决。出于某种原因,Mapbox 使用 READ_PHONE_STATE。我在 AndroidManifest.xml 中声明,但手动请求权限至关重要。

因此,解决方案:

public void requestReadPhoneStatePermission(Activity activity)
{
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
        {
            if (ActivityCompat.checkSelfPermission(
                    activity,
                    Manifest.permission.READ_PHONE_STATE
            ) != PackageManager.PERMISSION_GRANTED)
            {
                ActivityCompat.requestPermissions(
                        activity,
                        new String[]{
                                Manifest.permission.READ_PHONE_STATE
                        },
                        0x1
                );
            }
        }
}

并在 Application.java onCreate()

中调用
this.requestReadPhoneStatePermission(this);

尝试让文档中的示例正常工作,然后从那里继续。

https://docs.mapbox.com/android/maps/guides/install/