1

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

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

前任:

我的活动.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");
}

日志猫

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)

您对发生此异常时如何模拟/创建此力关闭有任何想法吗?

4

3 回答 3

3

如果您catch出现异常并打印堆栈跟踪,是的,您会收到“警告”并且应用程序不会崩溃 - 这就是捕获异常的关键。确保MyCustomException扩展RuntimeException(或使用 RuntimeException)并且不要将该isMenuButtonOk()方法声明为throws您的异常。

例如:

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");
    }
}
于 2013-08-02T16:47:06.287 回答
0

显示一个Dialog带有单个按钮的按钮,指示强制关闭,并为它的主体Button放置这个System.exit(1);

AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create();

alertDialog.setTitle("An error occured");

alertDialog.setMessage(<your-message>);

alertDialog.setButton("Force Close", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
             System.exit(1);
         }
    });

alertDialog.show();
于 2013-08-02T16:44:42.243 回答
0
try {
     ...
} catch (Exception e) {
  //some code
}

这将捕获您的应用程序所做的所有异常,因为它们都是异常的后代,请记住,这是一个非常贬值的方法

于 2013-08-02T17:05:23.430 回答