在 Notification Click 上将项目添加到片段内的 recyclerview

Adding item to recyclerview inside fragment on Notification Click

场景

我在 View Pager 中有 2 个 fragments。在其中一个片段中,我有一个按钮来生成通知。通知生成成功。现在我想发送一些关于通知点击的数据返回片段。为此,我在 activity 中使用 onNewIntent() 方法,并从中调用片段方法将数据传回片段。

一切正常。

问题:

从通知中收到数据后,我将其添加到 arraylist 并在 RecyclerView 中显示。现在的主要问题是,在这种情况下 onCreateView() 不会再次调用,并且由于这个问题,片段中的所有视图都是空的,这一直导致空指针异常。 有什么方法可以在 onResume() 中查看或保存对以前视图的引用。

这是相关代码。

Activity onNewIntent() 代码:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    intent=getIntent();
    if (intent.hasExtra("notification_data")) {
        Record record = (Record) intent.getSerializableExtra("notification_data");
        FragmentRecords fragmentRecords = new FragmentRecords();
        fragmentRecords.getDataFromIntent(record);
    }

}

片段getDataFromIntent方法:

    public void getDataFromIntent(Record record){

       DatabaseManager databaseManager=new DatabaseManager(MainActivity.context);
       recordDao = null;
       try {
           recordDao=databaseManager.getDao();
           addData();
       } catch (SQLException e) {
           e.printStackTrace();
       }
       recordArrayList.add(record);
       recordAdapter.notifyDataSetChanged();
   }

initView()方法

private void initView(View view){
    btnAddRecords= (Button) view.findViewById(R.id.btn_add_data);
    btnNotification= (Button) view.findViewById(R.id.btn_create_notification);
    recyclerRecords= (RecyclerView) view.findViewById(R.id.rv_list);
    recordArrayList=new ArrayList<>();
    DatabaseManager databaseManager=new DatabaseManager(MainActivity.context);
    recordDao = null;
    try {
        recordDao=databaseManager.getDao();
        addData();
    } catch (Exception e) {
        e.printStackTrace();
    }

addData() 方法包括将静态数据添加到数组列表中。

private void addData(){
    recordArrayList.add(new Record("Ram","1234567890","10 years","hello",1L));
    recordArrayList.add(new Record("Rahim","1234567890","12 years","hello",2L));
    recordArrayList.add(new Record("Shyam","1234567890","13 years","hello",3L));
    recordArrayList.add(new Record("Sunder","1234567890","40 years","hello",4L));
    recordArrayList.add(new Record("Demo","1234567890","14 years","hello",5L));
    recordArrayList.add(new Record("Demo1","1234567890","15 years","hello",6L));
    recordArrayList.add(new Record("Demo2","1234567890","16 years","hello",7L));
    for (int i=0;i<recordArrayList.size();i++){
        try {
            recordDao.create(recordArrayList.get(i));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    Toast.makeText(MainActivity.context, recordArrayList.size()+" records added successfully", Toast.LENGTH_SHORT).show();
   setAdapter();
}


private void setAdapter(){
       recordAdapter=new RecordAdapter(MainActivity.context,recordArrayList);
       RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(MainActivity.context);
       recyclerRecords.setLayoutManager(mLayoutManager);
       recyclerRecords.setItemAnimator(new DefaultItemAnimator());
       recyclerRecords.addItemDecoration(new SimpleItemDecorator(5));
       recyclerRecords.setAdapter(recordAdapter);


   }

错误堆栈跟踪

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference at com.testdemo.fragments.FragmentRecords.setAdapter(FragmentRecords.java:134) at com.testdemo.fragments.FragmentRecords.addData(FragmentRecords.java:128) at com.testdemo.fragments.FragmentRecords.getDataFromIntent(FragmentRecords.java:148) at com.testdemo.activities.MainActivity.onNewIntent(MainActivity.java:52) at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1265) at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1282) at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2755) at android.app.ActivityThread.performNewIntents(ActivityThread.java:2773) at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2782) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1602) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:227) at android.app.ActivityThread.main(ActivityThread.java:6102) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:961) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:822)

正如评论中指出的那样,您正在与一个尚未启动的新 FragmentRecords 实例交互,而不是使用您现有的片段。您应该尝试检索对现有 FragmentRecords 实例的引用(如果您使用标签来识别您的片段,则可能通过 findFragmentByTag 方法),然后在该实例上调用 getDataFromIntent

希望对您有所帮助。