Andorid开发遇到的问题集

来源:互联网 发布:mac文档管理 编辑:程序博客网 时间:2024/06/04 18:36
  • 1

    com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

    这个是在进行网络请求时出现的问题。
    在stackoverflow上也有人遇到了相同的问题,我按照他们的方法逐步修改

    • 1 Gson 和 retrofit 修改如下:

           Gson gson = new GsonBuilder()            .setLenient()            .create();    retrofit = new Retrofit.Builder()            .baseUrl(App.BASE_URL)            .client(client)            .addConverterFactory(GsonConverterFactory.create(gson))            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())            .build();

      这样上面的问题没有理却出现了新问题:

      com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

      这个问题很常见,只需要修改服务返回的对应结构就可以了。

  • 2

     Caused by: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20171123_152903.jpg (Permission denied)

    这是因为 6.0后读取文件需要手动检测读取权限,所以需要手动添加权限。

        private static final int REQUEST_EXTERNAL_STORAGE = 1;        private boolean mayRequestContacts() {            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {                  return true;            }            if (checkSelfPermission(READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED||                checkSelfPermission(WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {                  return true;            }            if (shouldShowRequestPermissionRationale(READ_EXTERNAL_STORAGE)||                shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE)) {                  requestPermissions(new String[]{READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE);             } else {                  requestPermissions(new String[]{READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE);            }            return false;          }        @Override          public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,                                         @NonNull int[] grantResults) {                if (requestCode == REQUEST_EXTERNAL_STORAGE) {                      if (grantResults.length == 1 &&                           grantResults[0] == PackageManager.PERMISSION_GRANTED) {                            mayRequestContacts();                      }                }          }
这样调用mayRequestContacts方法后获得权限就不会出现权限拒绝的情况了。
  • 3

    android.support.v7.internal.widget.ActionBarOverlayLayout

    不能预览布局了 如图:
    这里写图片描述

解决办法:切换them就好了

  • 4

    Error:Execution failed for task ':app:transformClassesWithDexForDebug'.com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

    Android工程方法数超过65535。官方提供的结局按办法

    • 导入multidex包,设置为支持多dex输出模式
             defaultConfig {                ...                multiDexEnabled true            }                   dependencies {              compile 'com.android.support:multidex:1.0.0'            }
* 覆写Application类如果工程中已经含有Application类,那么让它继承android.support.multidex.MultiDexApplication类, 

如果Application已经继承了其他类并且不想做改动,那么还有另外一种使用方式,覆写attachBaseContext()方法。
“`java
import android.support.multidex.MultiDex;

        ...        @Override        protected void attachBaseContext(Context base) {               super.attachBaseContext(base);               MultiDex.install(this);        }

“`

这样处理后,若代码函数超过65535, 就会生成多个dex文件。不会再报错。
  • 5
    更新到3.0.1后编译出现的问题。
    Warning:The android.dexOptions.incremental property is deprecated and it has no effect on the build process.

    修改gradle 如下:

    dexOptions {    incremental true    javaMaxHeapSize "4g"}
  • 6
    clean 后报错

    Error:Execution failed for task ‘:app:mockableAndroidJar’.
    java.nio.file.AccessDeniedException: /home/aven/.android/build-cache.lock

    1. Go to File -> Settings -> Build, Execution, Deployment -> Compiler
    2. Add to “Command-line Options”: -x :app:mockableAndroidJar
    3. Press “OK” and try to Rebuild Project again.