将项目添加到“通知”中的片段内部片段中.单击



方案

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

所有这些都可以正常工作。

问题:

接收到通知的数据后,我将其添加到arraylist并在RecyclerView中显示。现在的主要问题是,在这种情况下,onCreateView()不会再次调用,因此由于此问题,片段中的所有视图均为null,这始终导致Null指针异常。有什么方法可以在onResume()中获取视图或将引用保存到以前的视图中。

这是相关的代码。

活动 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);
    }
}

fragment 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()方法包括将静态数据添加到arraylist中。

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);

   }

错误stacktrace

java.lang.nullpointerexception:尝试调用虚拟方法'void android.support.v7.widget.recyclerview.setlaylayoutmanager(android.support.v7.widget.widget.recyclerview $ layoutmanager) 在com.testdemo.fragments.fragmentrecords.setadapter(fragmentRecords.java:134) 在com.testdemo.fragments.fragmentrecords.addata(fragmentRecords.java:128) 在com.testdemo.fragments.fragmentrecords.getdatafromintent(fragmentRecords.java:148) 在com.testdemo.activities.mainactivity.onnewintent(MainActivity.Java:52) at android.app.instrumentation.callactivityonnewintent(instrumentation.java:1265) at android.app.instrumentation.callactivityonnewintent(instrumentation.java:1282) 在android.app.act.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) 在java.lang.reflect.method.invoke(本机方法)上 在com.android.internal.os.os.zygoteinit $ methodAndargScaller.run(zygoteinit.java:961) 在com.android.internal.os.os.zygoteinit.main(zygoteinit.java:822)

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

我希望它有帮助。

相关内容

  • 没有找到相关文章