单击预测按钮时,应用程序崩溃
Posted
技术标签:
【中文标题】单击预测按钮时,应用程序崩溃【英文标题】:On clicking predict button, the app crashes 【发布时间】:2021-12-19 07:45:38 【问题描述】:最近我一直在做android studio,我建立了一个python图像分类模型,并希望将它与一个应用程序集成。但是当我点击预测按钮时,应用程序崩溃了。如果我能知道哪里出了问题,那将会很有帮助。 附件是代码和它抛出的错误 - Activitymain.java :-
package com.example.uni;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.uni.ml.ConvertedModel;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;
import java.io.IOException;
import java.nio.ByteBuffer;
public class MainActivity extends AppCompatActivity
private ImageView imgView;
private Button predict;
private Button select;
private TextView tv;
private Bitmap img;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgView = (ImageView) findViewById(R.id.imageView);
tv = (TextView) findViewById(R.id.textView);
select = (Button) findViewById(R.id.button);
predict = (Button) findViewById(R.id.button2);
select.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent,12);
);
predict.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
img = Bitmap.createScaledBitmap(img,
500,
500,
true);
try
ConvertedModel model = ConvertedModel.newInstance(getApplicationContext());
// Creates inputs for reference.
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]1, 500, 500, 3, DataType.FLOAT32);
TensorImage tensorImage = new TensorImage(DataType.FLOAT32);
tensorImage.load(img);
ByteBuffer byteBuffer = tensorImage.getBuffer();
inputFeature0.loadBuffer(byteBuffer);
// Runs model inference and gets result.
ConvertedModel.Outputs outputs = model.process(inputFeature0);
TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
// Releases model resources if no longer used.
model.close();
tv.setText((int) outputFeature0.getFloatArray()[0]);
catch (IOException e)
/* TODO Handle the exception */
);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 100)
imgView.setImageURI(data.getData());
Uri uri = data.getData();
try
img = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
catch (IOException e)
e.printStackTrace();
在 logcat 中我看到了这个 -
2021-11-05 13:23:38.040 24027-24027/com.example.uni I/tflite: Initialized TensorFlow Lite runtime.
2021-11-05 13:23:38.090 24027-24027/com.example.uni E/libc: Access denied finding property "ro.hardware.chipname"
2021-11-05 13:23:38.081 24027-24027/com.example.uni W/com.example.uni: type=1400 audit(0.0:232245): avc: denied read for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=14249 scontext=u:r:untrusted_app:s0:c48,c257,c512,c768 tcontext=u:object_r:vendor_default_prop:s0 tclass=file permissive=0
2021-11-05 13:23:38.841 24027-24027/com.example.uni E/com.example.un: Invalid ID 0x00000000.
2021-11-05 13:23:38.842 24027-24027/com.example.uni D/AndroidRuntime: Shutting down VM
2021-11-05 13:23:38.843 24027-24027/com.example.uni E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.uni, PID: 24027
android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:381)
at android.content.res.MiuiResources.getText(MiuiResources.java:97)
at android.widget.TextView.setText(TextView.java:6397)
at com.example.uni.MainActivity$2.onClick(MainActivity.java:85)
at android.view.View.performClick(View.java:7189)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7166)
at android.view.View.access$3500(View.java:819)
at android.view.View$PerformClick.run(View.java:27682)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7592)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
2021-11-05 13:23:38.887 24027-24027/com.example.uni I/Process: Sending signal. PID: 24027 SIG: 9
需要关于发生了什么以及如何解决它的建议。我正在使用 android studio arctic fox 2020.3.1 补丁 3。
【问题讨论】:
android.content.res.Resources$NotFoundException: String resource ID
- 你应该可以使用它进行调试。
【参考方案1】:
logcat 已经告诉你它在setText()
调用时崩溃了。你打电话给
tv.setText((int) outputFeature0.getFloatArray()[0]);
setText(int) 中的int
指的是在 string.xml (R.string.xxx
) 中定义的资源 ID。
如果您没有从 res 中获取字符串,则应使用 setText(CharSequence)。
你可能想要
tv.setText(String.format("%f",outputFeature0.getFloatArray()[0]));
(将“%f”更改为您想要的任何小数点或格式)
【讨论】:
好的,如果我错了,请纠正我。我从中了解到的是它需要字符串,而我们已经输入了整数。添加 'string.format' 基本上是告诉它获取整数值。这个答案很有帮助,因为预测按钮现在可以工作。非常感谢!!以上是关于单击预测按钮时,应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章