通知中的意图未正确发送到其他活动
Posted
技术标签:
【中文标题】通知中的意图未正确发送到其他活动【英文标题】:Intent from notification is not being sent correctly to other Activity 【发布时间】:2021-07-21 04:42:51 【问题描述】:我正在尝试在通知的 Intent 中添加额外内容,以便打开 ChatActivity 并与向我发送消息的用户进行对话。我在 NotificationsListener 上传递的用户不为空。问题是,无论我尝试什么其他调整,我都会不断收到此错误:
2021-04-27 21:46:57.679 13553-13553/com.example.videomeeting E/androidRuntime: FATAL EXCEPTION: main
Process: com.example.videomeeting, PID: 13553
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.videomeeting/com.example.videomeeting.activities.ChatActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.videomeeting.models.User.getUserName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3431)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7660)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.videomeeting.models.User.getUserName()' on a null object reference
at com.example.videomeeting.activities.ChatActivity.onCreate(ChatActivity.java:108)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7660)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
代码如下:
NotificationsListener.java
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage)
super.onMessageReceived(remoteMessage);
Intent activityIntent = new Intent(this, ChatActivity.class);
FirebaseDatabase.getInstance().getReference(KEY_COLLECTION_USERS)
.child(remoteMessage.getData().get(KEY_USER_ID))
.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot snapshot)
user = snapshot.getValue(User.class);
@Override
public void onCancelled(@NonNull DatabaseError error)
);
activityIntent.putExtra(Constants.INTENT_USER, user);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(
this,
0,
activityIntent,
FLAG_UPDATE_CURRENT
);
ChatActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
processExtraData();
//Toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
TextView usernameTV = findViewById(R.id.usernameTV);
usernameTV.setText(remoteUser.getUserName()); //ERROR LINE
@Override
protected void onResume()
super.onResume();
processExtraData();
@Override
public void onNewIntent(Intent intent)
super.onNewIntent(intent);
setIntent(intent);
processExtraData();
private void processExtraData()
remoteUser = (User) getIntent().getSerializableExtra(INTENT_USER);
我已经检查了 NotificationsListener 的 Intent 中的额外内容是否为空,而事实并非如此。一定有一个我没有看到的细节。感谢您的宝贵时间。
【问题讨论】:
【参考方案1】:报错信息明确表示remoteUser尚未初始化或为null,需要检查processExtraData()方法接收到什么数据和
如果 (User) getIntent().getSerializableExtra(INTENT_USER) 没有返回用户,那么问题出在 NotificationsListener 类中
【讨论】:
【参考方案2】:首先,确保您传递给活动的用户不为空。我认为onDataChange
在您将用户作为额外信息传递后触发。我认为它可能对您有所帮助,如果没有,请查看 this 和 this。
【讨论】:
【参考方案3】:谢谢你们,我只是在 onDataChange 中跟踪用户的值,由于它的异步性,这是不正确的。我做了这个修复以避免空值:
while(!isFound)
FirebaseDatabase.getInstance().getReference(KEY_COLLECTION_USERS)
.child(remoteMessage.getData().get(KEY_USER_ID))
.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot snapshot)
user = snapshot.getValue(User.class);
isFound = true;
@Override
public void onCancelled(@NonNull DatabaseError error)
);
再次感谢。
【讨论】:
以上是关于通知中的意图未正确发送到其他活动的主要内容,如果未能解决你的问题,请参考以下文章