Android中由于RuntimeException而阻止Activity关闭

问题描述 投票:0回答:2

我有一个 Java Android 应用程序,它使用 API 来使用 AI 识别图像。我的应用程序需要互联网连接才能使用 API 进行连接。当应用程序从 API 检索数据时,如果互联网连接丢失,则 Activity 将关闭并显示

RuntimeException

单击 Activity 中的按钮时执行的代码:

new RecogniseImage().execute(new File(photoPath));

这里,

photoPath
包含图像的路径。

RecogniseImage.java 文件:

//Connect to the API and retrieve data
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
                    .withCallCredentials(new ClarifaiCallCredentials(PAT));

            //Identify image
            MultiOutputResponse postModelOutputsResponseFood = stub.postModelOutputs(
               PostModelOutputsRequest.newBuilder().setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID)
                    .setAppId(APP_ID))
                    .setModelId(MODEL_ID_FOOD)
                    .addInputs(
                        Input.newBuilder().setData(
                            Data.newBuilder().setImage(
                                Image.newBuilder()
                                    .setBase64(ByteString.copyFrom(Files.readAllBytes(
                                            new File(IMAGE_FILE_LOCATION).toPath())))))).build());

            if (postModelOutputsResponseFood.getStatus().getCode() != StatusCode.SUCCESS) {
                throw new RuntimeException("Post model outputs failed, status: " + postModelOutputsResponseFood.getStatus());
            }

            Output outputFood = postModelOutputsResponseFood.getOutputs(0);

我尝试将调用 API 的代码包装在

try
catch
语句中,但 Activity 仍然崩溃。如果出现
RuntimeException
,有什么方法可以阻止 Activity 关闭吗?

我尝试将

try
catch
语句换行,如下所示:

try {
    new RecogniseImage().execute(new File(photoPath));
} 
catch (RuntimeException e){
   Log.d("Error","An error occurred");
}

或者像下面这样:

try {
     //Connect to the API and retrieve data
     V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
                    .withCallCredentials(new ClarifaiCallCredentials(PAT));

    //Identify image
    MultiOutputResponse postModelOutputsResponseFood = stub.postModelOutputs(            PostModelOutputsRequest.newBuilder().setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID)
                    .setAppId(APP_ID))
                    .setModelId(MODEL_ID_FOOD)
                    .addInputs(
                        Input.newBuilder().setData(
                            Data.newBuilder().setImage(
                                Image.newBuilder()
                                    .setBase64(ByteString.copyFrom(Files.readAllBytes(
                                            new File(IMAGE_FILE_LOCATION).toPath())))))).build());

            if (postModelOutputsResponseFood.getStatus().getCode() != StatusCode.SUCCESS) {
                throw new RuntimeException("Post model outputs failed, status: " + postModelOutputsResponseFood.getStatus());
            }

            Output outputFood = postModelOutputsResponseFood.getOutputs(0);
}
catch (RuntimeException e){
    Log.d("Error","An error occurred");
}

我想阻止 Activity 关闭并在出现任何异常时提醒用户。

欢迎任何帮助或建议。

java android android-activity runtimeexception
2个回答
-1
投票

删除这个:

throw new RuntimeException("Post model outputs failed, status: " + postModelOutputsResponseFood.getStatus());

-2
投票

您可以从下面的 URL 获取参考,他们使用单独的类来处理错误并扩展到您想要处理异常的类。
请看一下这里

© www.soinside.com 2019 - 2024. All rights reserved.