【发布时间】:2021-04-30 08:23:36
【问题描述】:
btPredict.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img= Bitmap.createScaledBitmap(img,200,200,true);
try {
Model model = Model.newInstance(getApplicationContext());
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 200, 200, 3},
DataType.FLOAT32);
TensorImage tensorImage = new TensorImage(DataType.FLOAT32);
tensorImage.load(img);
ByteBuffer byteBuffer = tensorImage.getBuffer();
inputFeature0.loadBuffer(byteBuffer);
Model.Outputs outputs = model.process(inputFeature0);
TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
textView.setText(outputFeature0.getFloatArray()[0] + " \n"+ outputFeature0.getFloatArray()[1]);
model.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我收到以下错误
021-04-30 11:55:07.70329735-29735/com.example.catsdogmodel E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.catsdogmodel, PID: 29735
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.example.catsdogmodel.MainActivity$3.onClick(MainActivity.java:94)
at android.view.View.performClick(View.java:7869)
at android.widget.TextView.performClick(TextView.java:14958)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7838)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29362)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8019)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)][1]
【问题讨论】:
-
什么都没有?没有解释什么失败了?没有错误信息?我们只需要猜测?您需要提供更多信息,以便我们能够猜出问题所在。
-
@JoachimSauer 我已经上传了错误。
-
getOutputFeature0AsTensorBuffer返回的值返回一个只有 1 个元素的 FloatArray。您尝试引用索引 0,然后引用索引 1,后者将失败。 -
@blagerweij 你的回答给了我这个错误 Array type expected;找到:'@org.checkerframework.checker.nullness.qual.NonNull float' 但谢谢
标签: java android tensorflow