【问题标题】:Intent is producing null object reference when moving from splash activity to main Activity从启动活动移动到主活动时,意图产生空对象引用
【发布时间】:2020-09-01 10:30:03
【问题描述】:

以前一切正常,现在当我运行我的应用程序时,它开始产生此错误并且我的应用程序不断崩溃。

此错误不断出现

android.content.Intent android.content.Intent.putExtra(java.lang.String, int)' 在空对象引用上

这是我的 Splash 活动代码。

Oncreate 方法代码如下

@覆盖 protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        FirebaseApp.initializeApp(SplashActivity.this);

        fragmentKey=0;
        intiViews();

        new Handler().postDelayed(() -> {

            redirectingIntent.putExtra("fragmentKey", fragmentKey);
            startActivity(redirectingIntent);
            finish();

        }, 3000);

InitViews 方法代码如下

    private void intiViews() {
        userRef = FirebaseDatabase.getInstance().getReference(Common.USER_REF);

        mAuth = FirebaseAuth.getInstance();

        //Check User if it exist
        if (mAuth.getCurrentUser() != null) {
            user = mAuth.getCurrentUser();
            CheckUserFromFirebase(user);
        } else {
            redirectingIntent = new Intent(SplashActivity.this, RegisterActivity.class);
            fragmentKey = 0;
        }

    }

    private void CheckUserFromFirebase(final FirebaseUser user) {
        userRef.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                if (dataSnapshot.exists()) {

                    UserModel userModel = dataSnapshot.getValue(UserModel.class);
                    updateUser(userModel);
                    redirectingIntent = new Intent(SplashActivity.this, MainActivity.class);
                    fragmentKey = 0;
                } else {
                    RegisterUser();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(SplashActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

在这里,我正在使用上传到 firebase 的用户模型更新我的用户

    private void updateUser(UserModel model) {
        Common.CurrentUser = model;
        Common.CurrentUser.setUid(user.getUid());
    }

如果没有给定uid的注册用户,则intent会去注册activity

    private void RegisterUser() {
        Toast.makeText(this, "No Data found", Toast.LENGTH_SHORT).show();
        redirectingIntent = new Intent(SplashActivity.this, RegisterActivity.class);
        fragmentKey = 1;
    }

}

【问题讨论】:

  • 我已经应用了无效捕获并重新启动,但仍然没有结果。
  • redirectingIntent 未初始化。检查您的条件并确保在处理程序执行之前初始化redirectingIntent
  • 感谢您的帮助,错误现已消除。

标签: android android-studio android-intent


【解决方案1】:
//update by this

 new Handler().postDelayed(() -> {
        if(redirectingIntent != null && fragmentKey != null)    
        {
            redirectingIntent.putExtra("fragmentKey", fragmentKey);
            startActivity(redirectingIntent);
            finish();
        }

    }, 3000);

【讨论】:

    【解决方案2】:

    首先,我会避免延迟处理程序,因为它容易出错;更确切地说是这段代码

    new Handler().postDelayed(() -> {
    
            redirectingIntent.putExtra("fragmentKey", fragmentKey);
            startActivity(redirectingIntent);
            finish();
    
    }, 3000);
    

    我会创建另一种方法来处理重定向并在需要时调用它

    private void redirectToNextActivity(fragmentKey: Integer) {
        if (redirectingIntent != null ) {
            redirectingIntent.putExtra("fragmentKey", fragmentKey);
            startActivity(redirectingIntent);
            finish();
        }
    }
    

    【讨论】:

    • 我正在使用延迟的 Handler() 只是因为闪屏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多