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