华为设备 Activity 的 onCreate() 中出现 NullPointerException

NullPointerException in onCreate() of Activity of Huawei devices

提问人:DeepikaJey 提问时间:9/13/2019 最后编辑:DeepikaJey 更新时间:9/18/2019 访问量:272

问:

我们将 firebase crashlytics 与我们的应用程序集成在一起,在最新的生产中记录了一个奇怪的异常。NullPointerException 发生在应用程序中 MainActivity 的 onCreate 中,堆栈跟踪如下。

 Attempt to invoke virtual method 'int android.content.Intent.getHwFlags()' on a null object reference.  
android.app.Activity.onCreate (Activity.java:1089)  
android.support.v4.app.SupportActivity.onCreate (ComponentActivity.java:75)
android.support.v4.app.FragmentActivity.onCreate (FragmentActivity.java:335)  
android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)  
com.mfiles.mobile.activity.Activity.onCreate (Activity.java:60)   
com.app.example.activity.MainMenuActivity.onCreate(MainMenuActivity.java:589)     
android.app.Activity.performCreate (Activity.java:7458)   
android.app.Activity.performCreate (Activity.java:7448)  
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1286)  
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3409)  
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3614)  
android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:86)  
android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:108)  
android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  
android.app.ActivityThread$H.handleMessage (ActivityThread.java:2199)  
android.os.Handler.dispatchMessage (Handler.java:112)  
android.os.Looper.loop (Looper.java:216)  
android.app.ActivityThread.main (ActivityThread.java:7625)  
java.lang.reflect.Method.invoke (Method.java)  
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)  
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:987)   

这是 MainMenuActivity 的 onCreate 方法,突出显示了碰撞线 589,它是 super.onCreate()。

protected void onCreate( Bundle savedInstanceState ) {

    // Treat the content of the window as secure.
    Utils.secureWindowContent( this );

    if( savedInstanceState != null && ! Constants.RESTORE_APPLICATION_FROM_SAVED_INSTANCE ) {

        // Application will not restore itself from saved instance but will do a re-login instead.
        mFinishedForReLogin = true;
        finish();
        Intent loginIntent = new Intent( MainMenuActivity.this, LoginActivity.class );
        startActivity( loginIntent );
        super.onCreate( null );
        return;
    }

    // Set context for possible debugging.
    FragmentId.sContext = this;

    boolean askPinAfterRestore = false;
    if( savedInstanceState != null ) {
        askPinAfterRestore = savedInstanceState.getBoolean( BUNDLE_ASK_PIN_CODE_AGAIN );
        mIsOauthLoggingin = savedInstanceState.getBoolean( BUNDLE_IS_OAUTH_IN_PROCESS );
    }

    restoreSession( askPinAfterRestore );

    // This prepareWindow must be called before super.onCreate().
    BusyIndicatorHandler.prepareWindow( new WeakReference< AppCompatActivity >( this ) );
    super.onCreate( savedInstanceState );  // Line: 589

    initialize();

    // If not restoring from system bundle, create initial fragment. On restore, fragments are already restored.
    if( savedInstanceState == null ) {
        initializeFirst();

        // If not restoring from system bundle, it means we come from LoginActivity, so the expiration check is done.
        // This flag is important, if some vault has expiration time set to 0 and this line is forgotten,
        // then the vault would stay inaccessible.
        mCheckExpirationOnRestart = false;
    }

    final ActionBar actionBar = getSupportActionBar();
    if( ! Utils.isLargeDisplay() && actionBar != null ) {
        actionBar.setBackgroundDrawable( new ColorDrawable( Color.parseColor( "#0e7ec8" ) ) );
    }

    // Configure shared preference file for saving offline objects.
    mOfflineSharedPreferenceFilename = getOfflineFilename();

    Utils.registerEvents( this );

    // Check for low storage.
    IntentFilter lowStorageFilter = new IntentFilter( Intent.ACTION_DEVICE_STORAGE_LOW );
    final boolean hasLowStorage = registerReceiver( null, lowStorageFilter ) != null;

    // Finish activity if storage is too low to running application normally.
    if( hasLowStorage ) {
        Toast.makeText( this, R.string.low_storage_alert, Toast.LENGTH_LONG ).show();
        Log.w( Constants.LOGTAG, getString( R.string.low_storage_alert ) );

        finish();
    }
}

我尝试查找 getHwFlags() 的文档,但不幸的是没有可用于此方法的文档,我想这是与在 Android 9 上运行的华为设备相关的一些自定义。

请帮助修复或防止崩溃。

Android-Activity nullPointerException 华为移动服务

评论

0赞 Nikos Hidalgo 9/13/2019
请在 Create 上发布您的活动,以防万一我们能发现其中的内容
0赞 Rafsanjani 9/13/2019
您是否尝试过迁移到AndroidX
0赞 Pankaj Kumar 9/13/2019
“MainMenuActivity.java:行号 589”是什么
0赞 Agnaramon 9/13/2019
向我们展示代码!
0赞 DeepikaJey 9/18/2019
使用 Activity 的 onCreate() 代码编辑问题。@Rafsanjani -> 我还没有尝试过迁移。

答: 暂无答案