Android 如何将我的通知详细信息添加到另一个片段中的回收器?
我有一个包含五个片段的活动,其中一个片段是一个通知片段,它有一个回收器视图并向其中添加项目,现在我有另一个片段,它有一个api请求并创建一个通知,该通知以通知的形式返回数据,我找不到从通知中获取数据并将其添加到通知片段中的回收器视图的方法 这是在片段上找到的我的通知代码,它必须在通知片段上找到的回收器视图中添加数据Android 如何将我的通知详细信息添加到另一个片段中的回收器?,android,android-fragments,android-recyclerview,Android,Android Fragments,Android Recyclerview,我有一个包含五个片段的活动,其中一个片段是一个通知片段,它有一个回收器视图并向其中添加项目,现在我有另一个片段,它有一个api请求并创建一个通知,该通知以通知的形式返回数据,我找不到从通知中获取数据并将其添加到通知片段中的回收器视图的方法 这是在片段上找到的我的通知代码,它必须在通知片段上找到的回收器视图中添加数据 //create an intent to open notification intent Intent
//create an intent to open notification intent
Intent intent=new Intent(getActivity (), NotificationFragmentCict.class);
NotificationFragmentCict fragment = new NotificationFragmentCict ();
FragmentManager fragmentManager = getFragmentManager();
//Replace intent with Bundle and put it in the transaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.nav_host_fragment_cict, fragment);
fragmentTransaction.replace(R.id.nav_host_fragment_cict,fragment);
fragmentTransaction.commit();
intent.setAction (Intent.ACTION_MAIN);
long when = System.currentTimeMillis ();
intent.addCategory (Intent.CATEGORY_LAUNCHER);
intent.putExtra ("controlnumber","Controlnumber:" + " " + studentClearance.getControlNumber () );
intent.putExtra ("reason","Reason: Full Transcript Fee");
intent.putExtra ("time"," " +when);
intent.putExtra ("department","From : SUA Bursar");
intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent statusPendingIntent = PendingIntent.getActivity (getActivity (),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationFragmentCict notificationFragmentCict =new NotificationFragmentCict ();
notificationFragmentCict.fillNotification (R.drawable.bursar, "Bursar", ""+studentClearance.getControlNumber (),"min");
NotificationCompat.Builder builder = new NotificationCompat.Builder (getActivity (), CHANNEL_IDcontrolnumber_Fulltranscript);
builder.setSmallIcon (R.drawable.ic_near_me_black_24dp);
builder.setContentTitle ("Control Number For Full Transcript");
builder.setPriority (NotificationCompat.PRIORITY_DEFAULT);
builder.setAutoCancel (true);
builder.setContentIntent (statusPendingIntent);
这是我的通知片段,它具有将新项目添加到回收器视图的功能
public void fillNotification(int mImage, String mtext, String mtext2, String mDate)
{
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager (getContext ());
mAdapter = new NotificationAdapter (notimodels);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
notimodels.add (0,new NotificationModel (mImage,mtext,mtext2,mDate));
swipeRefreshLayout.setRefreshing(false);
}
当我对其他片段使用填充通知时,它会抛出并出错
Process: com.univibezstudios.ocappservice.ocapp, PID: 23358
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at com.univibezstudios.ocappservice.ocapp.Fragments.NotificationFragmentCict.fillNotification(NotificationFragmentCict.java:87)
at com.univibezstudios.ocappservice.ocapp.Fragments.AccountFragmentCict$4$1$1.onDataChange(AccountFragmentCict.java:401)
事实上,这是一个棘手的过程
Intent
intent.putExtra(…)
挂起的意图
,并将您的活动意图附在其中通知中
intent.getIntExtra(..)
或intent.getStringExtra(..)
等获取intent数据RecylerView
你的意思是我只是在两个片段之间传递数据,但是我如何触发函数,它现在应该添加数据,但是当我调用fill notification方法check out have updated我的代码时,仍然得到相同的错误,我无法从包含recycler viewCall
notificationFragmentCict.fillNotification(…)
此活动的类之外向recycler添加新项目。通知中没有。因为活动只能创建它的片段。获取活动中的数据。然后,实例化活动中的片段,并将数据传递给片段!我在通知外部调用了该消息,但它抛出了相同的错误java.lang.NullPointerException:尝试在com.univibezstudios.ocapps.Fragments.NotificationFragmentCict.fillNotification上的空对象引用上调用虚拟方法“void androidx.recyclerview.widget.recyclerview.setHasFixedSize(布尔)”(NotificationFragmentCict.java:87)在com.univibezstudios.ocappservice.ocapp.Fragments.AccountFragmentCict.onCreateView(AccountFragmentCict.java:105)显示此错误是因为您的片段的视图尚未创建。这就是为什么recyclerview为null,并且您正在对null recyclerview调用setHasFixedSize(…)
方法的原因。