项目场景:
提示:看见项目代码中有一个System.arraycopy()函数以前没有用过,但是看源码注释也大概能了解意思,不过本着好记性不如烂键盘的话,还是自己敲敲为上。不敲不知道,一敲就敲出个bug
问题描述:
提示:报错如下
--------- beginning of crash
08-06 16:28:38.781 3400-3400/com.ppx.dailystudy E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ppx.dailystudy, PID: 3400
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ppx.dailystudy/com.ppx.dailystudy.wutong.WuTongDemoActivity}: java.lang.ArrayStoreException: source of type java.util.ArrayList is not an array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2570)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1420)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5606)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
Caused by: java.lang.ArrayStoreException: source of type java.util.ArrayList is not an array
at java.lang.System.arraycopy(Native Method)
at com.ppx.dailystudy.wutong.WuTongDemoActivity.testSystemCopyErr(WuTongDemoActivity.java:77)
at com.ppx.dailystudy.wutong.WuTongDemoActivity.onCreate(WuTongDemoActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6279)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2570)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1420)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5606)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
08-06 16:28:38.792 3400-3400/com.ppx.dailystudy I/Process: Sending signal. PID: 3400 SIG: 9
原因分析:
提示:后来发现这个函数貌似只能用于byte数组的copy(现在先不深究这个问题了/xk,如果有大佬路过且知道其原因的话,还请指点一二),于是修改类型为byte[],正常运行
解决方案:
提示:修改List为byte数组。
之前的代码:
private void testSystemCopyErr() {
List<String> stringList = new ArrayList<>();
stringList.add("我");
stringList.add("我");
stringList.add("我");
stringList.add("我");
stringList.add("我");
List<String> destList = new ArrayList<>();
Log.d(TAG, "testSystemCopyErr: copy前:" + destList.size());
System.arraycopy(stringList, 0, destList, 0, 5);
Log.d(TAG, "testSystemCopyErr: copy后:" + destList.size());
}
修改后的代码
public static void testSystemCopy() {
/**
* Object src
* int srcPos
* Object dest
* int destPos
* int length
*/
String str = "678";
int number = 102378978;
byte[] bytes;
byte[] bytes1 = new byte[3];
bytes = intToByteArray(number);
for (byte b : bytes1) {
Log.d("TAG", "testSystemCopy: 添加前size:" + b);
}
System.arraycopy(bytes, 0, bytes1, 0, 2);
for (byte b : bytes1) {
Log.d("TAG", "testSystemCopy: 添加后size:" + b);
}
}
public static byte[] intToByteArray(int value) {
int i = (byte) (value >> 24 & 0xFF);
int j = (byte) (value >> 16 & 0xFF);
int k = (byte) (value >> 8 & 0xFF);
return new byte[]{(byte) (value & 0xFF), (byte) k, (byte) j, (byte) i};
}