我有一个存储某些图像路径的ArrayList:
Map<String, Criterio> irregularidades = CriteriosAdapter.irregularidades;
for(Criterio c : irregularidades.values()) {
ArrayList<String> listFotos = c.getFotosList();
}
但是,如果应用程序通过电话锁或某些电话进行后台操作。当我尝试保存数据时,我收到了空指针异常。
有人可以帮我吗?
修改
我将添加完整代码以便于理解。
public class SaveFragment extends Fragment {
Map<String, String> detailsLoja = LojaDetailsActivity.detailsVistoria;
/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sync_import, container, false);
setHasOptionsMenu(true);
saveData();
return view;
}
@Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}
public void saveData() {
IrregularidadesDbAdapter mDb;
mDb = new IrregularidadesDbAdapter(getContext());
mDb.open();
int idVistoria = mDb.vistoriaInsert(detailsLoja.get("idBombeiro"), detailsLoja.get("idLoja"), detailsLoja.get("idArea"), changeDate(detailsLoja.get("vistoriaData")));
Map<String, Criterio> irregularidades = CriteriosAdapter.irregularidades;
for(Criterio c : irregularidades.values()) {
int idCriterio = c.getId();
int idEquipamento = c.getEquipamento();
int idArea = c.getSubArea;
ArrayList<String> listFotos = c.getFotosList();
int idIrregularidade = mDb.insert(idVistoria, idCriterio, idEquipamento, idArea);
for (int i = 0; i < listFotos.size(); i++) {
mDb.fotosInsert(idIrregularidade, listFotos.get(i));
}
}
mDb.close();
reloadActivity();
}
// Reload MainActivity
public void reloadActivity() {
Intent objIntent = new Intent(getContext(), MainActivity.class);
NavDrawerFragment.mConfiguration = Config.configuration();
NavDrawerFragment.mConfiguration = NavDrawerFragment.mConfiguration.subList(0, 6);
NavDrawerFragment.mConfiguration.add(0, new NavItem("Header", NavItem.TOP));
startActivity(objIntent);
}
}
答案 0 :(得分:0)
当片段必须在屏幕上加载UI时,方法onCreateView
仅被调用一次。因此,您需要在其他地方替换saveData();
。