Android应用程序在onCreate之前崩溃

时间:2017-06-08 23:55:08

标签: java android android-4.4-kitkat

我创建了一个可以在模拟器(API 25,API 19)和设备(API 23)上正常运行的应用程序。但是,当我尝试使用API​​ 19在KitKat设备上运行相同的应用程序时,应用程序崩溃。

我创建了一个所有其他活动都扩展的抽象基础活动。

public abstract class BaseAppCompatActivity extends AppCompatActivity {

    private Typeface OPEN_SANS_REGULAR = null;

    private Typeface getOpenSans() {
        if ( OPEN_SANS_REGULAR == null ) {
            OPEN_SANS_REGULAR = Typeface.createFromAsset( this.getAssets(), FontConstants.OPEN_SANS_REGULAR );
        }

        return OPEN_SANS_REGULAR;
    }

    /**
     * Set text to open Sans.
     *
     * @param view
     */
    protected void useOpenSans( TextView view ) {
        if ( OPEN_SANS_REGULAR == null ) {
            OPEN_SANS_REGULAR = Typeface.createFromAsset( this.getAssets(), FontConstants.OPEN_SANS_REGULAR );
        }

        if ( null != OPEN_SANS_REGULAR ) {
            view.setTypeface( OPEN_SANS_REGULAR );
        }
    }

    protected void useOpenSans( TextView... views ) {
        if ( OPEN_SANS_REGULAR == null ) {
            OPEN_SANS_REGULAR = Typeface.createFromAsset( this.getAssets(), FontConstants.OPEN_SANS_REGULAR );
        }

        if ( null != OPEN_SANS_REGULAR ) {

            for ( TextView view : views ) {
                view.setTypeface( OPEN_SANS_REGULAR );
            }
        }
    }

    protected void useOpenSans( MenuItem item ) {
        if( item != null ) {
            SpannableString spannableString = new SpannableString(item.getTitle());
            spannableString.setSpan(new OpenSansTypefaceSpan(getOpenSans()), 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            item.setTitle(spannableString);
        }
    }

    protected void useOpenSans( SubMenu menus ) {

        if( menus != null ) {
            int size = menus.size(), i = 0;

            for ( i = 0; i < size; i++ ) {
                useOpenSans( menus.getItem( i ) );
            }
        }
    }

    protected void useOpenSans( Menu menus ) {
        int size = menus.size(), i=0;

        for( i=0; i<size; i++ ) {
            MenuItem item =  menus.getItem( i );

            if( item != null ) {
                useOpenSans( item );
                useOpenSans( item.getSubMenu() );
            }
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransitionExit();
    }

    @Override
    public void startActivity( Intent intent ) {
        super.startActivity( intent );
        overridePendingTransitionEnter();
    }

    /**
     * Overrides the pending Activity transition by performing the "Enter" animation.
     */
    protected void overridePendingTransitionEnter() {
        overridePendingTransition( R.anim.slide_from_right, R.anim.slide_to_left );
    }

    /**
     * Overrides the pending Activity transition by performing the "Exit" animation.
     */
    protected void overridePendingTransitionExit() {
        overridePendingTransition( R.anim.slide_from_left, R.anim.slide_to_right );
    }

    public abstract String getTag();

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransitionExit();
    }

    /**
     * Wrapped On Create.
     * This is done to ensure that a mail is sent whenever an exception occurs.
     */
    public abstract void wrappedOnCreate( Bundle savedInstanceState );

    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );

        try{
            wrappedOnCreate( savedInstanceState );

        } catch ( Exception ex ){
            Log.e( getTag(), "Caught Exception ex " + ex.getMessage(), ex );
            Email email = new Email();

            String subject = "Caught Exception ex " + ex.getClass().getName() + " with message " + ex.getMessage();
            String body = ExceptionUtils.getStackTrace( ex );

            email.setSubject( subject )
                    .setBody( body );

            Mailer.send( email );
        }
    }
}

扩展此BaseAppCompactActivity的所有活动都不会覆盖onCreate,而是全部实现wrappedOnCreate,它被try catch块包围,如果发生异常则会发送邮件。它已经在仿真器和华硕设备上进行了测试,邮件确实有效。

然而,在HTC设备(API 19)HTC Desire 816G上,应用程序崩溃,没有电子邮件触发。我无法启动此设备将其连接到Android Monitor以查看LogCat。

正如我所看到的,由于没有触发电子邮件,这意味着android甚至可以在调用onCreate或wrappedOnCreate之前遇到错误?

可能导致这种情况的原因是什么?

0 个答案:

没有答案