当通过意图将字节数组从一个活动传递到另一个活动时,出现以下错误。我不确定是因为数组太大还是其他原因。这是错误:
StackOverflowError
在主要活动中,我有以下代码:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.roshan.android.picscheduler, PID: 19689
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.roshan.android.picscheduler/com.roshan.android.picscheduler.ImageDetectActivity}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.roshan.android.picscheduler.ImageDetectActivity.onCreate(ImageDetectActivity.java:40)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
在目标活动的onCreate方法中,我有这个:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mCameraView.setCameraListener(new CameraKitView.CameraListener() {
@Override
public void onOpened() {
}
@Override
public void onClosed() {
}
});
mCameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCameraView.captureImage(new CameraKitView.ImageCallback() {
@Override
public void onImage(CameraKitView cameraKitView, byte[] bytes) {
imageBytes = bytes;
}
});
Intent intent = new Intent(MainActivity.this.getApplicationContext(), ImageDetectActivity.class);
intent.putExtra("CapturedImage", imageBytes);
intent.putExtra("width", mCameraView.getWidth());
intent.putExtra("height", mCameraView.getHeight());
startActivity(intent);
//TODO: Launch new activity, pass the bitmap
// Maybe use Fragments instead of new activity?
}
});
}
我尝试解码字节数组时会引发错误,我认为这意味着它接收到空数组。
答案 0 :(得分:0)
发生错误是因为您从意图获得的字节数组为空。
1。。您从原始Activity传递了一个空字节数组。
2。。向意图中添加额外内容,然后从意图中将其取回时,您没有使用完全相同的String。
我建议调试您的代码,许多NullPointerExceptions
可以通过简单的调试来解决。
答案 1 :(得分:0)
此回调
mCameraView.captureImage(new CameraKitView.ImageCallback() {
@Override
public void onImage(CameraKitView cameraKitView, byte[] bytes) {
imageBytes = bytes;
}
});
被称为异步。这意味着,如果要将字节添加到意图中,则需要将意图代码移至该回调中。
需要将完整的onClick调用更改为此:
mCameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCameraView.captureImage(new CameraKitView.ImageCallback() {
@Override
public void onImage(CameraKitView cameraKitView, byte[] bytes) {
imageBytes = bytes;
Intent intent = new Intent(MainActivity.this.getApplicationContext(), ImageDetectActivity.class);
intent.putExtra("CapturedImage", imageBytes);
intent.putExtra("width", mCameraView.getWidth());
intent.putExtra("height", mCameraView.getHeight());
startActivity(intent);
}
});
}
});
这可能会解决将字节传递给下一个活动的问题,但是此代码也可能会导致用户交互(单击)和UI上发生的“填充”之间存在延迟,因此用户会感到延迟。您应该考虑对此进行改进(但这超出了此问题的范围)