1.java.net.SocketException: socket failed: EPERM (Operation not permitted)

出现原因:一开始网络请求时我没有赋予网络权限,我重新给程序赋予权限时我并没有卸载程序,只是重新编译,系统并没有获取到配置文件中的权限,因此需要卸载重新安装

2.recycrview包含edittext,滑动错乱问题?

在onBindViewHolder中这样设置TextWatcher

//1移除监听
        if(holder.stuAge.tag is TextWatcher) {
            holder.stuAge.removeTextChangedListener(holder.stuAge.tag as TextWatcher)
        }

        //2设置内容
        holder.stuAge.setText(student.age)

        val watch = object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
                student.age = p0.toString()
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
        }

        //3设置监听
        holder.stuAge.addTextChangedListener(watch)

        //4设置tag
        holder.stuAge.tag = watch

3.RecyclerView添加分割线?

a.添加默认分割线,即安卓自带的分割线

//添加Android自带的分割线
   recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

b.自定义分割线
打开DividerItemDecoration的源码

public void setDrawable(@NonNull Drawable drawable) {
        if (drawable == null) {
            throw new IllegalArgumentException("Drawable cannot be null.");
        }
        mDivider = drawable;
    }

我们只需调用这个方法,然后传入一个Drawable对象就可以完成自定义,现在用shape来编写一个分割线样式

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:startColor="@color/purple_200"
        android:centerColor="@color/teal_200"
        android:endColor="@color/design_default_color_primary"
        android:type="linear"/>

    <size android:height="3dp"/>


</shape>

添加代码改成如下

//添加自定义分割线
        DividerItemDecoration divider = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this,R.drawable.custom_divider));
        recyclerView.addItemDecoration(divider);

4.json转实体类的在线网站

https://www.bejson.com/json2javapojo/new/

http://www.jsons.cn/json2java/

http://tool.chinaz.com/Tools/json2entity.aspx

5.安卓对象传递错误

Android中的Activity传递数据时,为了方便往往将很多数据封装成对象,然后将整个对象传递过去。传对象的时候有两种情况,一种是实现Parcelable接口,一种是实现Serializable接口。
可以用bundle putSerializable(Key,Object)传递数据或者直接用intent putExtrr(Key,Object)传递数据。

今天我在进行传递数据的时候遇到了问题。

1、抛出java.io.NotSerializableException异常

抛出这个异常是因为你的对象没有实现Serializable接口,只要实现该接口就好了。

2、抛出java.lang.RuntimeException异常

抛出这个异常是因为传递的对象里面的对象也要实现Serializable接口。

6. java.net.SocketTimeoutException

okhttp超时问题,设置超时时间无效时将以下代码

OkHttpClient client = new OkHttpClient();
        client.newBuilder().connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS);

改成

OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .build();

7.Android Studio 无法识别同一个 package 里的其他类,将其显示为红色,但是 compile 没有问题。鼠标放上去后显示 “Cannot resolve symbol XXX”

1.Build -> Clean Project
2.Build -> Rebuild Project
3.某些 setting 出了问题,点击菜单中的 “File” -> “Invalidate Caches / Restart”,然后点击对话框中的 “Invalidate and Restart”,清空 cache 并且重启。语法就会正确的高亮了。

8.应用市场上架经检测含有【a.gray.BulimiaTGen.f】病毒

一般是腾讯手机管家报的错,确认无病毒可以申诉

9.AAR 引入AAR出现Direct local .aar file dependencies are not supported

1.在你的工程目录下创建一个文件夹local_repo,然后把aar文件放入,然后在local_repo目录下创建一个build.gradle文件,内容如下:

configurations.maybeCreate("default")
artifacts.add("default", file('***.aar'))

2.在settings.gradle导入该工程
3.在你需要依赖的工程里的build.gradle里增加依赖

知识点

1.Android提供了丰富的Dialog函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮)、列表、单选、多选、等待、进度条、编辑、自定义等多种形式。
有时,我们希望在对话框创建或关闭时完成一些特定的功能,这需要复写Dialog的create()、show()、dismiss()等方法。
2.1 普通Dialog
2个按钮

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button buttonNormal = (Button) findViewById(R.id.button_normal);
        buttonNormal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNormalDialog();
            }
        });
    }
    
    private void showNormalDialog(){
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象,因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog = 
            new AlertDialog.Builder(MainActivity.this);
        normalDialog.setIcon(R.drawable.icon_dialog);
        normalDialog.setTitle("我是一个普通Dialog")
        normalDialog.setMessage("你要点击哪一个按钮呢?");
        normalDialog.setPositiveButton("确定", 
            new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //...To-do
            }
        });
        normalDialog.setNegativeButton("关闭", 
            new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //...To-do
            }
        });
        // 显示
        normalDialog.show();
    }
}

3个按钮

/* @setNeutralButton 设置中间的按钮
 * 若只需一个按钮,仅设置 setPositiveButton 即可
 */
private void showMultiBtnDialog(){
    AlertDialog.Builder normalDialog = 
        new AlertDialog.Builder(MainActivity.this);
    normalDialog.setIcon(R.drawable.icon_dialog);
    normalDialog.setTitle("我是一个普通Dialog").setMessage("你要点击哪一个按钮呢?");
    normalDialog.setPositiveButton("按钮1", 
        new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // ...To-do
        }
    });
    normalDialog.setNeutralButton("按钮2", 
        new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // ...To-do
        }
    });
    normalDialog.setNegativeButton("按钮3", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // ...To-do
        }
    });
    // 创建实例并显示
    normalDialog.show();
}

2.2 列表Dialog

private void showListDialog() {
    final String[] items = { "我是1","我是2","我是3","我是4" };
    AlertDialog.Builder listDialog = 
        new AlertDialog.Builder(MainActivity.this);
    listDialog.setTitle("我是一个列表Dialog");
    listDialog.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // which 下标从0开始
            // ...To-do
            Toast.makeText(MainActivity.this, 
                "你点击了" + items[which], 
                Toast.LENGTH_SHORT).show();
        }
    });
    listDialog.show();
}

2.3 单选Dialog

int yourChoice;
private void showSingleChoiceDialog(){
    final String[] items = { "我是1","我是2","我是3","我是4" };
    yourChoice = -1;
    AlertDialog.Builder singleChoiceDialog = 
        new AlertDialog.Builder(MainActivity.this);
    singleChoiceDialog.setTitle("我是一个单选Dialog");
    // 第二个参数是默认选项,此处设置为0
    singleChoiceDialog.setSingleChoiceItems(items, 0, 
        new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            yourChoice = which;
        }
    });
    singleChoiceDialog.setPositiveButton("确定", 
        new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (yourChoice != -1) {
                Toast.makeText(MainActivity.this, 
                "你选择了" + items[yourChoice], 
                Toast.LENGTH_SHORT).show();
            }
        }
    });
    singleChoiceDialog.show();
}

2.4 多选Dialog

ArrayList<Integer> yourChoices = new ArrayList<>();
private void showMultiChoiceDialog() {
    final String[] items = { "我是1","我是2","我是3","我是4" };
    // 设置默认选中的选项,全为false默认均未选中
    final boolean initChoiceSets[]={false,false,false,false};
    yourChoices.clear();
    AlertDialog.Builder multiChoiceDialog = 
        new AlertDialog.Builder(MainActivity.this);
    multiChoiceDialog.setTitle("我是一个多选Dialog");
    multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
        new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which,
            boolean isChecked) {
            if (isChecked) {
                yourChoices.add(which);
            } else {
                yourChoices.remove(which);
            }
        }
    });
    multiChoiceDialog.setPositiveButton("确定", 
        new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            int size = yourChoices.size();
            String str = "";
            for (int i = 0; i < size; i++) {
                str += items[yourChoices.get(i)] + " ";
            }
            Toast.makeText(MainActivity.this, 
                "你选中了" + str, 
                Toast.LENGTH_SHORT).show();
        }
    });
    multiChoiceDialog.show();
}

2.5 等待Dialog

private void showWaitingDialog() {
    /* 等待Dialog具有屏蔽其他控件的交互能力
     * @setCancelable 为使屏幕不可点击,设置为不可取消(false)
     * 下载等事件完成后,主动调用函数关闭该Dialog
     */
    ProgressDialog waitingDialog= 
        new ProgressDialog(MainActivity.this);
    waitingDialog.setTitle("我是一个等待Dialog");
    waitingDialog.setMessage("等待中...");
    waitingDialog.setIndeterminate(true);
    waitingDialog.setCancelable(false);
    waitingDialog.show();
}

2.6 进度条Dialog

private void showProgressDialog() {
    /* @setProgress 设置初始进度
     * @setProgressStyle 设置样式(水平进度条)
     * @setMax 设置进度最大值
     */
    final int MAX_PROGRESS = 100;
    final ProgressDialog progressDialog = 
        new ProgressDialog(MainActivity.this);
    progressDialog.setProgress(0);
    progressDialog.setTitle("我是一个进度条Dialog");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setMax(MAX_PROGRESS);
    progressDialog.show();
    /* 模拟进度增加的过程
     * 新开一个线程,每个100ms,进度增加1
     */
    new Thread(new Runnable() {
        @Override
        public void run() {
            int progress= 0;
            while (progress < MAX_PROGRESS){
                try {
                    Thread.sleep(100);
                    progress++;
                    progressDialog.setProgress(progress);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
            // 进度达到最大值后,窗口消失
            progressDialog.cancel();
        }
    }).start();
}

2.7 编辑Dialog

private void showInputDialog() {
    /*@setView 装入一个EditView
     */
    final EditText editText = new EditText(MainActivity.this);
    AlertDialog.Builder inputDialog = 
        new AlertDialog.Builder(MainActivity.this);
    inputDialog.setTitle("我是一个输入Dialog").setView(editText);
    inputDialog.setPositiveButton("确定", 
        new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this,
            editText.getText().toString(), 
            Toast.LENGTH_SHORT).show();
        }
    }).show();
}

2.8 自定义Dialog

<!-- res/layout/dialog_customize.xml-->
<!-- 自定义View -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        />
</LinearLayout>
private void showCustomizeDialog() {
    /* @setView 装入自定义View ==> R.layout.dialog_customize
     * 由于dialog_customize.xml只放置了一个EditView,因此和图8一样
     * dialog_customize.xml可自定义更复杂的View
     */
    AlertDialog.Builder customizeDialog = 
        new AlertDialog.Builder(MainActivity.this);
    final View dialogView = LayoutInflater.from(MainActivity.this)
        .inflate(R.layout.dialog_customize,null);
    customizeDialog.setTitle("我是一个自定义Dialog");
    customizeDialog.setView(dialogView);
    customizeDialog.setPositiveButton("确定",
        new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // 获取EditView中的输入内容
            EditText edit_text = 
                (EditText) dialogView.findViewById(R.id.edit_text);
            Toast.makeText(MainActivity.this,
                edit_text.getText().toString(),
                Toast.LENGTH_SHORT).show();
        }
    });
    customizeDialog.show();
}

3.复写回调函数
/* 复写Builder的create和show函数,可以在Dialog显示前实现必要设置

  • 例如初始化列表、默认选项等
  • @create 第一次创建时调用
  • @show 每次显示时调用
    */
private void showListDialog() {
    final String[] items = { "我是1","我是2","我是3","我是4" };
    AlertDialog.Builder listDialog = 
        new AlertDialog.Builder(MainActivity.this){
        
        @Override
        public AlertDialog create() {
            items[0] = "我是No.1";
            return super.create();
        }

        @Override
        public AlertDialog show() {
            items[1] = "我是No.2";
            return super.show();
        }
    };
    listDialog.setTitle("我是一个列表Dialog");
    listDialog.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // ...To-do
        }
    });
    /* @setOnDismissListener Dialog销毁时调用
     * @setOnCancelListener Dialog关闭时调用
     */
    listDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
        public void onDismiss(DialogInterface dialog) {
            Toast.makeText(getApplicationContext(),
                "Dialog被销毁了", 
                Toast.LENGTH_SHORT).show();
        }
    });
    listDialog.show();
}