Android 錯誤解決方案集錦


1、Caused by: java.lang.ClassCastException: androidx.multidex.MultiDexApplication cannot be cast to com.example.firecontrol.MainApplication

解決方法,在manifest.xml中添加:

<application

    android:name=".MainApplication"

2、Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class com.google.android.material.tabs.TabLayout

     Caused by: java.lang.reflect.InvocationTargetException

需要將activity當前的theme繼承Theme.AppCompat

<style name="AppBaseTheme" parent="Theme.AppCompat.Light" />

 

<style name="AppTheme" parent="AppBaseTheme">

    <item name="android:textColor">@color/colorBlack</item>

    <item name="android:windowBackground">@color/colorWhite</item>

    <item name="colorPrimary">@color/colorPrimary</item>

    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

    <item name="colorAccent">@color/colorAccent</item>

</style>

3、Java異常:java.lang.InstantiationException

出現這個異常可能有很多原因,我的原因是把class用abstact修飾了

4、java.lang.BootstrapMethodError: Exception from call site #1 bootstrap method

android {

   ······

    compileOptions{

        sourceCompatibility JavaVersion.VERSION_1_8

        targetCompatibility JavaVersion.VERSION_1_8

    }

 }

5、Android studio 3.5格式化xml層級錯亂問題

Settings > Editor > Code Style > XML > Set from… > Predefined Style > Android

6、com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: For input string

問題出現的原因:

javaBean中有一個變量是Integer

json對象是String

7、Static interface methods are only supported starting with Android N (--min-api 24)

在build.gradle文件Android下添加compileOptions

android {

   ······

    compileOptions{

        sourceCompatibility JavaVersion.VERSION_1_8

        targetCompatibility JavaVersion.VERSION_1_8

    }

 }

8、Couldn't find meta-data for provider with authority

問題出現原因:缺少下載權限

<!-- 7.0下載權限 -->
<provider
       android:name="androidx.core.content.FileProvider"
       android:authorities="自定義"
       android:exported="false"
       android:grantUriPermissions="true">

<!-- 元數據 -->
<meta-data
       android:name="android.support.FILE_PROVIDER_PATHS"
       android:resource="@xml/file_path" />
</provider>

<service android:name=".download.DownloadService" />

9、PackageInstaller: Requesting uid 10215 needs to declare permission android.permission.REQUEST_INSTALL_PACKAGES

解決辦法:mainfest中添加適配權限

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> <!-- 適配8.0 -->

10、java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=65813, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.cloudXF.app/com.cloudXF.app.index.MainActivity}: java.lang.RuntimeException: Cannot execute method locationTask because it is non-void method and/or has input parameters.

出現場景:Android 6.0 以上使用EasyPermissions動態申請權限

//AfterPermissionGranted只能與不帶參數的方法一起使用,否則會崩潰@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    // Forward results to EasyPermissions
    EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}

@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
}

@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
}

@AfterPermissionGranted(Constant.REQUEST_CODE.LOCATION)
public void locationTask() {//我的錯誤原因:locationTask方法攜帶參數
    String[] perms = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
    if (EasyPermissions.hasPermissions(getActivity(), perms)) {
        locate();
    } else {
        EasyPermissions.requestPermissions(this, getString(R.string.location_permission), Constant.REQUEST_CODE.LOCATION, perms);
    }
}

11、BuildConfig.DEBUG一直返回false

我的錯誤原因:自動導入的BuildConfig不是應用程序中,而是導入的其他庫的BuildConfig

12、RecyclerView報錯

(1)java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 4(offset:4).state:10 androidx.recyclerview.widget.RecyclerView{e9d59c4 VFED..... ......I. 0,108-174,858 #7f080179 app:id/rv_floor}, adapter:com.cloudXF.app.business.home.adapter.FloorAdapter@bf120ad, layout:androidx.recyclerview.widget.LinearLayoutManager@6dcfe2, context:com.cloudXF.app.index.MainActivity@ee48f73

解決方法:重寫LinearLayoutManager

public class CommonLayoutManager extends LinearLayoutManager {
    public CommonLayoutManager(Context context) {
        super(context);
    }

    public CommonLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public CommonLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            super.onLayoutChildren(recycler, state);
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }
}

13、Canvas:Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed

報錯代碼:canvas.clipRect(mClipBounds, Region.Op.UNION);

解決方案:

  if  (Build.VERSION.SDK_INT >= 26) {
      canvas.clipRect(mClipBounds);
  }  else {
      canvas.clipRect(mClipBounds, Region.Op.UNION);
  }

14、Error:Could not resolve all dependencies for configuration ':classpath'

原因分析:Android Studio 3.0 中 Google 將自己的依賴包放在了 google ( ),我的項目沒有添加google()倉庫

解決方案:

  repositories {
      google() //Google倉庫
      jcenter() //JCenter倉庫
  }

15、Your project path contains non-ASCII characters.

原因:項目路經包含中文

16、andorid布局文件里面的app:srcCompat設置的圖片不顯示

(1)Activity 繼承AppcompatActivity ,使用 ImageView的 app:srcCompat 是沒有問題的

(2)Activity 不是繼承AppcompatActivity,用AppCompatImageView 代替 ImageView

17、集成百度地圖時報錯Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'jniLibs' for source set 'main' of type org.gradle.api.internal.tasks.DefaultSourceSet.

原因:將sourceSets放在了build.gradle的最外層

sourceSets {
    main {
        jniLibs.srcDir 'libs'
        jni.srcDirs = [] //disable automatic ndk-build
    }
}

修改為:放在android{}中

android {
    ……
    sourceSets {
       main {
          jniLibs.srcDir 'libs'
          jni.srcDirs = [] //disable automatic ndk-build
       }
    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM