fragments
一個
View Pager
內
。在其中一個片段中,我有一個按鈕來生成通知。通知已成功生成。現在我想發送一些關於通知的數據,然後回到片段。爲此,我在activity
內使用onNewIntent()
方法,並從中調用片段方法將數據傳回片段。
這一切工作正常。
問題:
從通知接收到數據之後,我將它添加到一個arraylist
和表示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()方法由將靜態數據添加到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);
}
錯誤堆棧跟蹤
顯示java.lang.NullPointerException:嘗試調用虛擬方法「無效android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $ LayoutManager)'在com.testdemo.fragments.FragmentRecords.setAdapter(FragmentRecords.java:134) 處的null對象引用 處com.testdemo.fragments.FragmentRecords.addData(FragmentRecords.java:128) at 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) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread $ H.handleMessage(ActivityThread。(android.app.ActivityThread.main(ActivityThread.java:java:1602) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:227) at android.app.ActivityThread.main 6102) 在java.lang.reflect.Method.invoke(本機方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:961) 在com.android.internal.os.ZygoteInit 。主要(ZygoteInit.java:822)
你可以發佈錯誤stacktrace? –
錯誤爲空指針,因爲它不調用onCreateView,因此當它移動到setAdapter方法時,recyclerview爲空,因此它崩潰 –
在onNewIntent中,您正在創建FragmentRecords的新意圖,並在全新對象上調用getDataFromIntent函數。 – nnn