设想
我fragments
在一个View Pager
. 在其中一个片段中,我有一个按钮来生成通知。通知已成功生成。现在我想在通知点击返回片段时发送一些数据。为此,我在onNewIntent()
内部使用方法,activity
并从中调用片段方法将数据传递回片段。
所有这一切都很好。
问题:
从通知接收数据后,我将其添加到 anarraylist
并显示在RecyclerView
. 现在的主要问题是在这种情况下onCreateView()
不会再次调用,并且由于这个问题,片段中的所有视图都是空的,这一直导致空指针异常。有什么方法可以查看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);
}
}
片段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:尝试在 com.testdemo.fragments 的空对象引用上调用虚拟方法 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)'。 FragmentRecords.setAdapter(FragmentRecords.java:134) 在 com.testdemo.fragments.FragmentRecords.addData(FragmentRecords.java:128) 在 com.testdemo.fragments.FragmentRecords.getDataFromIntent(FragmentRecords.java:148) 在 com.testdemo.activities .MainActivity.onNewIntent(MainActivity.java:52) 在 android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1265) 在 android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1282) 在 android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2755) 在 android。 app.ActivityThread.performNewIntents(ActivityThread.java:2773) 在 android.app.ActivityThread.handleNewIntent(ActivityThread.java:2782) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1602) 在 android.os.Handler.dispatchMessage(Handler.java:111) 在 android.os.Looper.loop(Looper.java:227) 在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)