I'm trying to toggle between adding and removing the same linearLayout view from my fragment. Everytime I add the line of code:
((ViewGroup)indexLayout.getParent()).removeView(indexLayout)
I get a null pointer exception, even when trying to add the view back...
OfflineFragment.java
private void displayIndex() {
indexLayout = (LinearLayout)v.findViewById(R.id.side_index);
List<String> indexList = new ArrayList<>(mapIndex.keySet());
TextView textView;
for (String index : indexList) {
textView = (TextView) getActivity().getLayoutInflater().inflate(R.layout.alphabet_indicator, null);
textView.setText(index);
textView.setOnClickListener(this);
indexLayout.addView(textView);
}
}
private void showDialog() {
Dialog deleteDialog = new AlertDialog.Builder(getActivity()).setTitle("Warning:")
.setMessage("Are you sure that you want to delete all offline contacts?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Delete cached data from internal storage and clear data structures
File dir = getActivity().getFilesDir();
File file = new File(dir, "cached.txt");
boolean deleted = file.delete();
System.out.println(deleted);
listView.setAdapter(null);
cachedSearch.clear();
l.clear();
mapIndex.clear();
sections = null;
names = null;
((ViewGroup)indexLayout.getParent()).removeView(indexLayout)
}
}
)
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}
).create();
deleteDialog.show();
}
Stack Trace:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
at com.fil.uk.mobile.fidelitycontacts.Fragments.OfflineFragment$override.displayIndex(OfflineFragment.java:122)
at com.fil.uk.mobile.fidelitycontacts.Fragments.OfflineFragment$override.refreshData(OfflineFragment.java:215)
at com.fil.uk.mobile.fidelitycontacts.Fragments.OfflineFragment$override.access$dispatch(OfflineFragment.java)
at com.fil.uk.mobile.fidelitycontacts.Fragments.OfflineFragment.refreshData(OfflineFragment.java:0)
at com.fil.uk.mobile.fidelitycontacts.CarouselFragment.refreshData(CarouselFragment.java:215)
at com.fil.uk.mobile.fidelitycontacts.MainActivity.update(MainActivity.java:120)
at com.fil.uk.mobile.fidelitycontacts.Fragments.SearchResultFragment.refreshData(SearchResultFragment.java:212)
at com.fil.uk.mobile.fidelitycontacts.Fragments.SearchResultFragment.onCreateView(SearchResultFragment.java:105)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
If you remove view
Than you cant get it back !! you have to add it agin if you want to use it
Try to add View if null