简体   繁体   English

android.content.Context.getPackageName() 在 null object 参考

[英]android.content.Context.getPackageName() on a null object reference

I am getting this error when transtioning from a fragment to an activity as shown below:从片段转换到活动时出现此错误,如下所示:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference         at android.content.ComponentName.<init>(ComponentName.java:130)         at android.content.Intent.<init>(Intent.java:6108)

Below is my code for going to the next activity, The error occurs on the first line of the code below.下面是我进入下一个活动的代码,错误发生在下面代码的第一行。

  Intent mainIntent = new Intent (getContext(), MainActivity.class);
            mainIntent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity (mainIntent);

I don't see any solution so far online.到目前为止,我在网上没有看到任何解决方案。

It seems that you're getting a wrong context that raises this NullPointerException似乎你得到了一个错误的上下文,引发了这个NullPointerException

Try to replace the below line:尝试替换以下行:

Intent mainIntent = new Intent (getContext(), MainActivity.class);

With: >> if you're within an activity与:>>如果您在活动中

Intent mainIntent = new Intent (this, MainActivity.class);

with: >> if you're within a callback listener within the activity与:>>如果您在活动中的回调侦听器中

Intent mainIntent = new Intent (MyActivityName.this, MainActivity.class);     

With: >> if you're within a fragment与:>>如果你在一个片段内

Intent mainIntent = new Intent (requireActivity(), MainActivity.class);

Please try with getActivity() intsead of getContext()请尝试使用 getContext() 的 getActivity() intsead

I work the transaction of the fragment with this code我使用此代码处理片段的事务

private Context context;

context.startActivity(new Intent(context, MainActivity.class)); 

I have been able to find a work around this.我已经能够找到解决这个问题的方法。 I realised I am getting the error because I am creating the new Intent inside the firebase OnCompleteListener as shown below.我意识到我收到了错误,因为我正在 firebase OnCompleteListener 中创建新 Intent,如下所示。 So after i removed it from inside the listener and called it outside, my program works properly.所以在我从监听器内部删除它并在外面调用它之后,我的程序可以正常工作。

I instead created a global boolean variable that I updated to true on successful data storage.相反,我创建了一个全局 boolean 变量,我在成功存储数据时将其更新为 true。 Then I can access it somewhere else and then transtion to another if true.然后我可以在其他地方访问它,然后如果为真则转换到另一个地方。

 #BEFORE

    PostsRef.child(key).updateChildren(postsMap)
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if (task.isSuccessful()) {
                                                Toast.makeText(getActivity(), "New Post is updated successfully.", Toast.LENGTH_SHORT).show();
                                                Intent mainIntent = new Intent (getActivity(), MainActivity.class);
                                                mainIntent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                                startActivity (mainIntent);
                                            } else {
                                                Toast.makeText(context, "Error occured while updating your post.", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
#AFTER

 PostsRef.child(key).updateChildren(postsMap)
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                        mSuccess = true;
                                        Toast.makeText(getActivity(), "New Post is updated successfully.", Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(context, "Error occured while updating your post.", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });

Hello guys I have also realised one of the big causes of this error is the lifecycle methods.So I was overriding them inside my fragment to update the user state on the firebase database.大家好,我也意识到这个错误的主要原因之一是生命周期方法。所以我在我的片段中覆盖它们以更新 firebase 数据库上的用户 state。 Because of this they caused the android.content.Context.getPackageName() on a null object reference error whenever I tried to move to the main activity managing it.因此,每当我尝试转到管理它的主要活动时,它们android.content.Context.getPackageName() on a null object reference错误。 Incase I used the fragment transition to move to another fragment, the MainActivity would get excecuted more than twice.如果我使用片段转换移动到另一个片段,则 MainActivity 将被执行两次以上。 After stopping to override them my application worked properly.停止覆盖它们后,我的应用程序正常工作。 If someone would explain why that happens it would be great.如果有人能解释为什么会发生这种情况,那就太好了。

   @Override
        public void onStart() {
            super.onStart();
    updateUserState("Online");
        }
@Override
        public void onResume() {
            super.onResume();
    updateUserState("Online");
        }

Since, you are calling from a Fragment , you should use getActivity() here instead of getContext()由于您是从Fragment调用的,因此您应该在此处使用getActivity()而不是 getContext()

Intent mainIntent = new Intent (getActivity(), MainActivity.class);
        mainIntent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity (mainIntent);

This should help I hope.我希望这应该会有所帮助。

Sometimes startActivity method throws an exception like:有时 startActivity 方法会抛出一个异常,例如:

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。 Is this really what you want?这真的是你想要的吗?

So you should intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) method所以你应该intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 空对象引用上的android.content.Context.getPackageName()导致应用崩溃 - app crash with android.content.Context.getPackageName()' on a null object reference 空对象引用错误时出现&#39;java.lang.String android.content.Context.getPackageName()&#39; - 'java.lang.String android.content.Context.getPackageName()' on a null object reference error 尝试通过java类访问活动中的方法时,null对象引用上的android.content.Context.getPackageName()&#39; - android.content.Context.getPackageName()' on a null object reference, when trying to access a method in an activity through a java class 空对象引用上的DialogFragment&#39;java.lang.String android.content.Context.getPackageName()&#39; - DialogFragment 'java.lang.String android.content.Context.getPackageName()' on a null object reference 尝试在 null object 引用错误上调用虚拟方法 'java.lang.String android.content.Context.getPackageName()' - Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference error 运行时Android应用程序崩溃:“ java.lang.String android.content.Context.getPackageName()” - Android app crashing on run: 'java.lang.String android.content.Context.getPackageName()' 当意图数据发送到另一个Activity android.content.Context.getPackageName()时发生异常 - Exception when Intent Data to another Activity android.content.Context.getPackageName() 由于尝试调用虚拟方法&#39;java.lang.String android.content.Context.getPackageName(),因此意图失败 - Intent fails because of Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName() 尝试调用虚拟方法&#39;java.lang.String android.content.Context.getPackageName() - Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName() android.content.Context.getResources() 在空对象引用上 - android.content.Context.getResources() on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM