weixin_42178608
小白学代码之错题小本本
2021-01-09 10:22

Android Studio FATAL EXCEPTION: main

  • android-studio

Android Studio 点击运行可以在手机上正常运行,但在模拟器上运行就报错和闪退。FATAL EXCEPTION: main

报错:
2021-01-09 10:10:45.732 26984-26984/com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapp, PID: 26984
    java.lang.ExceptionInInitializerError
        at okhttp3.internal.platform.Platform.get(Platform.java:85)
        at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:263)
        at okhttp3.OkHttpClient.<init>(OkHttpClient.java:229)
        at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:1015)
        at cn.bmob.v3.http.This.<init>(BmobClient.java:126)
        at cn.bmob.v3.http.This.I(BmobClient.java:138)
        at cn.bmob.v3.http.mine$This.Code(RxBmob.java:418)
        at cn.bmob.v3.http.thing.Code(BmobFactory.java:346)
        at cn.bmob.v3.Bmob.getServerTime(Bmob.java:204)
        at cn.bmob.v3.BmobWrapper.Code(BmobWrapper.java:60)
        at cn.bmob.v3.Bmob.Code(Bmob.java:64)
        at cn.bmob.v3.Bmob.initialize(Bmob.java:41)
        at com.example.myapp.activity.Splash.onCreate(Splash.java:30)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        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:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.IllegalStateException: Expected Android API level 21+ but was 30
        at okhttp3.internal.platform.AndroidPlatform.buildIfSupported(AndroidPlatform.java:238)
        at okhttp3.internal.platform.Platform.findPlatform(Platform.java:202)
        at okhttp3.internal.platform.Platform.<clinit>(Platform.java:79)
        at okhttp3.internal.platform.Platform.get(Platform.java:85) 
        at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:263) 
        at okhttp3.OkHttpClient.<init>(OkHttpClient.java:229) 
        at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:1015) 
        at cn.bmob.v3.http.This.<init>(BmobClient.java:126) 
        at cn.bmob.v3.http.This.I(BmobClient.java:138) 
        at cn.bmob.v3.http.mine$This.Code(RxBmob.java:418) 
        at cn.bmob.v3.http.thing.Code(BmobFactory.java:346) 
        at cn.bmob.v3.Bmob.getServerTime(Bmob.java:204) 
        at cn.bmob.v3.BmobWrapper.Code(BmobWrapper.java:60) 
        at cn.bmob.v3.Bmob.Code(Bmob.java:64) 
        at cn.bmob.v3.Bmob.initialize(Bmob.java:41) 
        at com.example.myapp.activity.Splash.onCreate(Splash.java:30) 
        at android.app.Activity.performCreate(Activity.java:8000) 
        at android.app.Activity.performCreate(Activity.java:7984) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        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:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

Splash.java的第三十行是:
```
    Bmob.initialize(this,"abaaf362bc8f971c2fac1dda83407c35");
```
            
源代码如下:

```
package com.example.myapp.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapp.MainActivity;
import com.example.myapp.R;

import java.util.Timer;
import java.util.TimerTask;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;

public class Splash extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        //延时操作
        Timer timer = new Timer();
        timer.schedule(timetast, 2000);

        Bmob.initialize(this,"abaaf362bc8f971c2fac1dda83407c35");


    }
        TimerTask timetast = new TimerTask() {
            @Override
            public void run() {
               // startActivity(new Intent(Splash.this, MainActivity.class));
                //如果已经登陆,跳转到主界面,若没有登陆,跳转到登陆界面
                BmobUser bmobUser = BmobUser.getCurrentUser(BmobUser.class);
                if (bmobUser!=null){
                    startActivity(new Intent(Splash.this,MainActivity.class));
                    finish();
                }else
                    //没有登陆则显示登陆界面
                    startActivity(new Intent(Splash.this,Login.class));
                finish();


            }
        };

}

```
在Manifest中应该也配置了:

```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">

    <!--允许联网 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!--获取GSM(2g)、WCDMA(联通3g)等网络状态的信息  -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!--获取wifi网络状态的信息 -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!--保持CPU 运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载 -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!--获取sd卡写的权限,用于文件上传和下载-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!--允许读取手机状态 用于创建BmobInstallation-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />


    <application

        android:networkSecurityConfig="@xml/network_security_config"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/zhifa"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyAPP">


        <provider
            android:name="cn.bmob.v3.util.BmobContentProvider"
            android:authorities="com.example.myapp.BmobContentProvider">
        </provider>

        <activity android:name=".activity.Splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity"/>
        <activity android:name=".activity.Login" />
        <activity android:name=".activity.Register" />
        <activity android:name=".activity.Recive" />
        
    </application>

</manifest>
```

实在是不知道如何解决,请求大佬们解答!谢谢!!!

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答