gpt4 book ai didi

android - 发生自定义异常时创建强制关闭

转载 作者:行者123 更新时间:2023-11-29 17:57:52 25 4
gpt4 key购买 nike

对于一个 android 库项目,我正在尝试创建一个 Force Close 以向该库的用户显示他忘记做某事。

为此,我尝试创建我的自定义异常,并抛出它们,但我只有 LogCat 中的一些警告,仅此而已。

例如:

MyActivity.java

protected void onResume() {
super.onResume();
try {
this.isMenuButtonOk();
} catch(MyCustomException e) {
e.printStackTrace();
}
}
private void isMenuButtonOk() throws MyCustomException{
if(!this.mOnCreateOptionsMenuHasBeenCalledFlag)
throw new MyCustomException("OnCreateOptionMenu() needs to call it's parent. Or you need to deactivate the use of the menubutton");
}

LogCat

08-02 18:28:13.507    3866-3866/com.example.testlibrary W/System.err: com.example.testlibrary.MyCustomException: OnCreateOptionMenu() needs to call it's parent. Or you need to deactivate the use of the menubutton
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at com.example.testlibrary.lib.MyActivity.isMenuButtonOk(MyActivity.java:286)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at com.example.testlibrary.lib.MyActivity.onResume(MyActivity.java:244)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at com.example.testlibrary.MainActivity.onResume(MainActivity.java:304)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at android.app.Activity.performResume(Activity.java:5082)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at android.os.Looper.loop(Looper.java:137)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4745)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
08-02 18:28:13.511 3866-3866/com.example.testlibrary W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-02 18:28:13.515 3866-3866/com.example.testlibrary W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-02 18:28:13.515 3866-3866/com.example.testlibrary W/System.err: at dalvik.system.NativeStart.main(Native Method)

当发生此异常时,您是否知道如何模拟/创建此强制关闭?

最佳答案

如果您捕获异常并打印堆栈跟踪,是的,您会收到“警告”并且应用程序不会崩溃 - 这就是捕获异常的意义所在。确保 MyCustomException 扩展 RuntimeException(或使用 RuntimeException)并且不要声明 isMenuButtonOk() 方法 throws你的异常(exception)。

例如:

protected void onResume() {
super.onResume();
this.isMenuButtonOk();
}

private void isMenuButtonOk(){
if(!this.mOnCreateOptionsMenuHasBeenCalledFlag){
throw new RuntimeException("OnCreateOptionMenu() needs to call it's parent. Or you need to deactivate the use of the menubutton");
}
}

关于android - 发生自定义异常时创建强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18022063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com