1

Android ライブラリ プロジェクトの場合、このライブラリのユーザーが何かをするのを忘れたことを示す強制終了を作成しようとしています。

そうするために、カスタム例外を作成しようとしましthrowたが、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)

この例外が発生したときに、この強制終了をシミュレート/作成する方法について何か考えはありますか?

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 に答える