Acces BottomNavigationView item from onItemClick in a fragment - android

I have a MainActivity with a BottomNavigationView with 3 items in it. When I first open the app, only one is enable, the first one.
I have a RecyclerView on the first fragment (the first item in the menu). When I click on one item of the RecyclerView, I want to switch to the second Fragment and to enable the second and third item.
I am able to switch to the second Fragment, but I can not set the items enable again.
The first Fragment with the RecyclerView is:
public class Homefragment extends Fragment implements OnItemClickListener {
private ArrayList<fileItem> mfileList = new ArrayList<>();
private RecyclerView mRecyclerView;
private fileAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private BottomNavigationView bottomNav;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
Context context = getActivity();
bottomNav = view.findViewById(R.id.bottom_navigation);
// Listing all text files
String path = Environment.getExternalStorageDirectory().toString()+"/FlexiCounts";
File directory = new File(path);
File[] filesList = directory.listFiles(new FilenameFilter() {
public boolean accept(File directory, String name) {
return name.toLowerCase().endsWith(".txt");
}
});
setRecyclerView(view, filesList);
return view;
}
private void setRecyclerView(View view, File[] files){
if (files.length > 0) {
for (int i = 0; i < files.length; i++) {
mfileList.add(new fileItem(R.drawable.ic_account_balance_black_24dp, files[i].getName().replace(".txt", ""), "25-03-2020", "07-04-2020"));
}
mRecyclerView = view.findViewById(R.id.fileRecycler);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mAdapter = new fileAdapter(mfileList, this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}
// the magic should happen here !
#Override
public void onItemClick(int position) {
mfileList.get(position);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container_primary, new Dashboardfragment()).commit();
bottomNav.getMenu().getItem(1).setEnabled(true);
//bottomNav.getMenu().getItem(2).setEnabled(true);
}
}
The following lines does not work:
bottomNav.getMenu().getItem(1).setEnabled(true);
It seems to point to a null reference. I think it does not find the bottomNav define with R.id.bottom_navigation in the onCreateView method. In fact, the bottom_navigation is contained in the MainActivity layout, but that should not be a problem. I get this error:
--------- beginning of crash
2020-04-15 20:09:20.202 8673-8673/com.flexicounts.flexicounts E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.flexicounts.flexicounts, PID: 8673
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Menu android.support.design.widget.BottomNavigationView.getMenu()' on a null object reference
at com.flexicounts.flexicounts.Homefragment.onItemClick(Homefragment.java:145)
at com.flexicounts.flexicounts.fileAdapter$fileViewHolder.onClick(fileAdapter.java:50)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

create a BottomNavigationView getBottomNav() method in main activity then get it from onItemClick
in MainActivity:
public BottomNavigationView getBottomNav(){ return findViewById(R.id.bottom_navigation); }
in Homefragment:
// the magic should happen here !
#Override
public void onItemClick(int position) {
mfileList.get(position);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container_primary, new Dashboardfragment()).commit();
((MainActivity) getActivity()).getBottomNav().getMenu().getItem(1).setEnabled(true);
//bottomNav.getMenu().getItem(2).setEnabled(true);
}

Related

How to resolve error findViewById error in android fragment

I'm trying to add recyclerview in my fragment but I'm getting error when I click on navigation drawer item.
Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.myapplication.fragment.Books.onCreate(Books.java:45)
How to resolve error or suggest tutorial to load json data in fragment using recyclerview. with click event on recyclerview. on click item open new fragment with data.
This is my code
public class Books extends Fragment {
private BooksModel booksViewModel;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<BooksModel> bookLists;
// Context context;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerViewBook); //Line 45 Error
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
bookLists = new ArrayList<>();
for(int i=0; i<=10; i++){
BooksModel bookList = new BooksModel(
"",
"Title " + (i + 1),
"This is Auther"
);
bookLists.add(bookList);
adapter = new BooksAdapter(bookLists,this);
recyclerView.setAdapter(adapter);
}
}
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
booksViewModel =
new ViewModelProvider(this).get(BooksModel.class);
View root = inflater.inflate(R.layout.fragment_books, container, false);
final TextView textView = root.findViewById(R.id.text_book );
booksViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
onCreate() runs before onCreateView(). You should move all of that code to onViewCreated(), which passes you the View directly (meaning you don't have to call getView() at all).

Opening new fragment from RecyclerView button within a fragment

I have a fragment running that has part of its layout view load a RecyclerView Card. From within this card I want to launch a new fragment that opens a full screen layout.
public class MemoriesFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MemListFragment fragment = new MemListFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.placeholder, fragment);
fragmentTransaction.commit();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_authoring_memory_menu_1, container, false);
return view;
}}
That's the main fragment. The next one is the fragment that creates and populates the RecyclerView
public class MemListFragment extends Fragment {
ImageButton button1;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_authoring_memory_list, container,false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.listRecyclerView);
ListAdapter listAdapter = new ListAdapter();
recyclerView.setAdapter(listAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
button1 = view.findViewById(R.id.memory_image);
return view;
}}
This is the RecyclerView
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private LayoutInflater layoutInflater;
private List<String> data;
public ImageButton memory_image;
public Adapter(MemoriesFragment Context, List<String> data){
// this.layoutInflater = LayoutInflater.from();
this.data = data;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = layoutInflater.inflate(R.layout.memory_view,viewGroup,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
// bind the textview with data received
String title = data.get(i);
viewHolder.textTitle.setText(title);
// similarly you can set new image for each card and descriptions
viewHolder.memoryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AppCompatActivity activity = (AppCompatActivity) view.getContext();
Fragment MemoryDetails = new MemoryDetails();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.placeholder, MemoryDetails).addToBackStack(null).commit();
}
});
}
#Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// Define UI components here
TextView textTitle;
ImageButton memoryButton;
public ViewHolder(#NonNull View itemView) {
super(itemView);
textTitle = itemView.findViewById(R.id.textTitle);
memoryButton = itemView.findViewById(R.id.memory_image);
}
}
}
The recylerview is created with memory_view.xml which just has text and an imagebutton named memory_image. When I run this code it crashes when I click the Image Button.
Edit: added error log below
2020-02-21 16:50:29.334 19558-19558/com.app.emotivo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.emotivo, PID: 19558
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatImageButton with id 'memory_image'
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:434)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:391)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
i am not sure if it works but can you try using supportfragmentmanager in the memories fragment too , and cast your the the result of itemView.findViewById(R.id.memory_image)to an ImageButton like that (ImageButton)itemView.findViewById(R.id.memory_image)
the display of the fragment inside the adapter is not a good thing.
I think first of all use the navigation on Android Jetpack
Or use RxJava and transfer the formatting representation from adapter to fragment.

Blank Fragment inside Tablayout / Viewpager

So i'm desperately looking for an answer to my problem. I spend hours try and retry some things but that did not work.
So here is my problem :
I have only one activity for my application which is the main activity.
This activity contains a bottom navigation bar and a frame layout to load a fragment depending the choice of the item on the bottom navigation bar.
My PredictionsFragment (loaded by bottom navigation bar) contains a TabLayout (using SmartTabLayout library). In this Fragment, a list of data is loaded in a thread because it use internet connection. TabLayout is containing PredictionsViewFragment which displays a RecyclerView.
When I select the PredictionsFragment on my bottom navigation bar, tablayout and all the recyclerview are perfectly displayed.
But when I go to another item and go back to PredictionsFragment it displays a blank view...
PredictionsFragment code :
public class PredictionsFragment extends Fragment {
private static List<Country> countryList = new ArrayList<>();
private ViewPager viewPager;
private SmartTabLayout viewPagerTab;
private FragmentStatePagerItemAdapter adapter;
private FragmentPagerItems pages;
private ProgressBar progressBar;
private ViewGroup tab;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_predictions_tab, container, false);
initView(view);
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (countryList.size() == 0) {
refresh();
} else {
refreshViewPager();
}
}
private void initView(View view) {
tab = (ViewGroup) view.findViewById(R.id.tab);
tab.addView(LayoutInflater.from(getContext()).inflate(R.layout.fragment_view_predictions, tab, false));
progressBar = (ProgressBar) view.findViewById(R.id.progressbar);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
viewPagerTab = (SmartTabLayout) view.findViewById(R.id.viewpagertab);
viewPager.setOffscreenPageLimit(countryList.size());
pages = new FragmentPagerItems(getContext());
adapter = new FragmentStatePagerItemAdapter(getFragmentManager(), pages){
public int getItemPosition(Object object) {
return POSITION_NONE;
}
};
viewPager.setAdapter(adapter);
viewPagerTab.setViewPager(viewPager);
}
private void refresh() {
// Thread car connexion internet
new Thread(new Runnable() {
#Override
public void run() {
countryList = InternetData.getCountryList();
refreshViewPager();
}
}).start();
}
private void refreshViewPager() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
for (Country country : countryList) {
pages.add(FragmentPagerItem.of(country.getCode(), PredictionsViewFragment.class));
}
adapter.notifyDataSetChanged();
viewPagerTab.setViewPager(viewPager);
progressBar.setVisibility(View.GONE);
}
});
}
public static List<Prediction> getPredictionList(int index) {
return countryList.get(index).getPredictionList();
}
}
PredictionsViewFragment code :
public class PredictionsViewFragment extends Fragment {
private RecyclerView recyclerView;
private PredictionAdapter adapter;
private int position;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recycler_view_pronos, container, false);
position = FragmentPagerItem.getPosition(getArguments());
initRecyclerView(view);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
private void initRecyclerView(View view) {
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
adapter = new PredictionAdapter(PredictionsFragment.getPredictionList(position));
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
}
}
Screenshot of the blank fragment and the app
EDIT :
When I use getChildFragmentManager() method :
adapter = new FragmentStatePagerItemAdapter(getChildFragmentManager(), pages){
public int getItemPosition(Object object) {
return POSITION_NONE;
}
};
I get this error when I go the second time on the Fragment (The first time I go on it, it works perfectly) :
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:936)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:217)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1461)
at android.view.View.dispatchRestoreInstanceState(View.java:18608)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3821)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3827)
at android.view.View.restoreHierarchyState(View.java:18586)
at android.support.v4.app.Fragment.restoreViewState(Fragment.java:494)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1486)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
EDIT 2 :
I added these lines to the onViewCreated method on my PredictionsFragment :
else {
refreshViewPager();
}
But it does absolutely nothing... The name of the tab appears properly but not the recyclerview...
It's like the Fragment inside the tablayout isn't loaded
EDIT 3 :
I have added a SwipeRefreshLayout to my recyclerview and for the 2 first tabs i can't do a pull to refresh and from the 3rd tabs only one item of the recyclerview appears and pull to refresh "works" but it displays only one item in the recyclerview
If you have any questions tell me I'll do my best to answer it !
Thanks in advance for you help I appreciate it
Use setOffsetScreenPageLimit in viewPager: This will help you to store the data in the pagers fragments when you navigate through bottom tabs.
viewPager.setOffscreenPageLimit(the number of tab you are setting);
For refreshing the data in recycler view you can use "Swipe refresh"
So I solved my problem...
You have to use this :
adapter = new FragmentPagerItemAdapter(getChildFragmentManager(), pages);
Use FragmentPagerItemAdapter and NOT FragmentStatePagerItemAdapter
And for sur you have to use getChildFragmentManager()
I was also using BottomNavigationView to open tabLayout with viewPager & faced the same problem. FragmentStatePagerAdapter can be used for this.
This class handles saving and restoring of fragment's state -> It's in the documentation

Android Studio - Update RecyclerView from Dialog Fragment in Menu Item

Hey guys I really need your help. I've spent like 5 days trying to get my recyclerview to update only when the user presses OK on a dialogbox, that appears from the menu actionbar. I've tried every possible method I could think of, every method I've seen on stackoverflow, YouTube, etc. and none of them worked for me.
How do I get the recyclerview in a fragment to update after dialogbox closes? I know there are similar questions regarding updating the menu, and (recyclerviews with dialogfragments), but none of them have a combination.
Out of the countless attempts, the current code configuration posted below isn't causing any errors, however, the recyclerview remains blank. The closest attempt I had to finding a solution, was creating an adapter and setting up the recycler in onOptionsItemSelected. But obviously, it updates only when the user clicks the button, and the initial click would create a blank recyclerview.
Fragment:
(There's a lot of repeated commented code from different attempts)
public class ExerciseRoutine extends Fragment implements ExerciseRoutine_Dialog.RoutineDialogListener{
private String Routine_name, Routine_split;
private ArrayList<ExerciseRoutine_Information> Routine_information = new ArrayList<>();
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
#Override
public void sendInput(String name, String split, RecyclerView.Adapter DialogAdapter) {
Routine_name = name;
Routine_split = split;
adapter = DialogAdapter;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.exercise_routine_fragment, container, false);
//Report that this fragment would like to participate in populating the options menu by
//receiving a call to onCreateOptionsMenu(Menu, MenuInflater) and related methods.
//If true, the fragment has menu items to contribute.
setHasOptionsMenu(true);
recyclerView = view.findViewById(R.id.ExerciseRoutine_Recycler);
//BuildRecyclerView();
//recyclerView.setHasFixedSize(true); //If the Recyclerview is static
/*Routine_information.add(new ExerciseRoutine_Information(Routine_name, Routine_split));
recyclerView = view.findViewById(R.id.ExerciseRoutine_Recycler);
//recyclerView.setHasFixedSize(true); //If the Recyclerview is static
layoutManager = new LinearLayoutManager(getActivity());
adapter = new ExerciseRoutineAdapter(Routine_information);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);*/
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.exercise_routine_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_addRoutine:
ExerciseRoutine_Dialog routineDialog = new ExerciseRoutine_Dialog();
routineDialog.setTargetFragment(ExerciseRoutine.this, 1);
routineDialog.show(getFragmentManager(), "Routine Dialog");
//Routine_information.add(new ExerciseRoutine_Information(Routine_name, Routine_split));
BuildRecyclerView();
//adapter.notifyItemInserted(Routine_information.size());
//if(!Routine_name.equals("") && !Routine_split.equals("")) {
//}
}
return super.onOptionsItemSelected(item);
}
public void BuildRecyclerView(){
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
public void BuildAdapter(){
//adapter = new ExerciseRoutineAdapter(getContext(),Routine_information);
adapter.notifyItemInserted(Routine_information.size());
}
}
My Dialog Fragment:
public class ExerciseRoutine_Dialog extends DialogFragment{
private TextView ActionOK, ActionCANCEL;
private EditText Routine_name, Routine_split;
private RoutineDialogListener activityCommander;
private ArrayList<ExerciseRoutine_Information> Routine_information = new ArrayList<>();
private RecyclerView.Adapter adapter;
public interface RoutineDialogListener{
void sendInput(String name, String split, RecyclerView.Adapter DialogAdapter);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try{
activityCommander = (RoutineDialogListener) getTargetFragment();
}catch(ClassCastException e){
throw new ClassCastException(context.toString() + "Must Implement RoutineDialogListener");
}
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.exercise_routine_dialog, container, false);
Routine_name = view.findViewById(R.id.ExerciseRoutine_DialogNameInput);
Routine_split = view.findViewById(R.id.ExerciseRoutine_DialogSplitInput);
ActionOK = view.findViewById(R.id.ExerciseRoutine_DialogAction_OK);
ActionCANCEL = view.findViewById(R.id.ExerciseRoutine_DialogAction_CANCEL);
//recyclerView = view.findViewById(R.id.ExerciseRoutine_Recycler);
ActionCANCEL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getDialog().dismiss();
}
});
ActionOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = Routine_name.getText().toString();
String split = Routine_split.getText().toString();
if(!name.equals("") && !split.equals("")) {
Routine_information.add(new ExerciseRoutine_Information(name, split));
adapter = new ExerciseRoutineAdapter(getContext(), Routine_information);
activityCommander.sendInput(name, split, adapter);
adapter.notifyItemInserted(Routine_information.size());
}
getDialog().dismiss();
}
});
return view;
}
}
Your current approach seems to be to pass the RecyclerView.Adapter to the DialogFragment and try to insert the new data on-the-spot. I think this is a problematic setup. The dialog's purpose is to offer some means for the users to enter the required data, period. It should not be tasked with the job of managing the RecyclerView or its Adapter because that way your components will be very tightly coupled:
Imagine that first you use a ListView in your implementation, and suddenly someone decides to ban every ListView from your app (maybe for performance reasons) and has you exchange them all for RecyclerViews. Then your approach would force you to change the code for the DialogFragment (it would have to cater to a different type of Adapter). A more loosely coupled implementation would enable you to make changes to one component without having to rewrite too many others.
That's why I won't try to make your code work as-is, instead I'd like to show you another way:
Because the RecyclerView is part of the Fragment's UI, the Fragment is the place where code related to managing the RecyclerView belongs. It is basically possible to have the Adapter as an inner class of the Fragment but I prefer having it as a standalone class if the code gets a little longer, and also because it enforces "decoupling".
Interfaces play a very important part in good architecture, so the DialogFragment will still make use of an interface to send its data. But it's up to the class which actually implements the interface (here: the Fragment) to pass the data to any interested third parties, e.g. the RecyclerView.Adapter (The Adapter in turn could have its own interface to publish important events like clicks on list items).
Having said that, here are some code snippets:
The DialogFragment
public class ExerciseRoutine_Dialog extends DialogFragment {
private EditText Routine_name, Routine_split;
public interface RoutineDialogListener{
/**
* There is some new ExerciseRoutine_Information
*/
void sendInput(String name, String split);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.exercise_routine_dialog, container, false);
Routine_name = view.findViewById(R.id.ExerciseRoutine_DialogNameInput);
Routine_split = view.findViewById(R.id.ExerciseRoutine_DialogSplitInput);
TextView actionOK = view.findViewById(R.id.ExerciseRoutine_DialogAction_OK);
TextView actionCANCEL = view.findViewById(R.id.ExerciseRoutine_DialogAction_CANCEL);
actionCANCEL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getDialog().dismiss();
}
});
actionOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = Routine_name.getText().toString();
String split = Routine_split.getText().toString();
if(!name.equals("") && !split.equals("")) {
// just send the input to the main Fragment
RoutineDialogListener listener = getListener();
if(listener != null) {
listener.sendInput(name, split);
}
}
getDialog().dismiss();
}
});
return view;
}
/**
* Tries to find a suitable listener, examining first the hosting Fragment (if any) and then the Activity.
* Will return null if this fails
* #return x
*/
private RoutineDialogListener getListener(){
RoutineDialogListener listener;
try{
Fragment onInputSelected_Fragment = getTargetFragment();
if (onInputSelected_Fragment != null){
listener = (RoutineDialogListener) onInputSelected_Fragment;
}
else {
Activity onInputSelected_Activity = getActivity();
listener = (RoutineDialogListener) onInputSelected_Activity;
}
return listener;
}catch(ClassCastException e){
Log.e("Custom Dialog", "onAttach: ClassCastException: " + e.getMessage());
}
return null;
}
}
The Fragment:
public class ExerciseRoutine extends Fragment implements ExerciseRoutine_Dialog.RoutineDialogListener{
public static final String ROUTINE_DIALOG = "Routine Dialog";
private ArrayList<ExerciseRoutine_Information> routineInformations = new ArrayList<>();
private RecyclerView.Adapter adapter;
public static ExerciseRoutine instance(){
return new ExerciseRoutine();
}
#Override
public void sendInput(String name, String split) {
routineInformations.add(new ExerciseRoutine_Information(name, split));
adapter.notifyItemInserted(routineInformations.size());
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.exercise_routine_fragment, container, false);
setHasOptionsMenu(true);
RecyclerView recyclerView = view.findViewById(R.id.ExerciseRoutine_Recycler);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
adapter = new ExerciseRoutineAdapter(getContext(), routineInformations);
// So far you have a RecyclerView with an empty List.
recyclerView.setAdapter(adapter);
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.exercise_routine_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_addRoutine:
showDialog();
return true;
}
return super.onOptionsItemSelected(item);
}
private void showDialog(){
ExerciseRoutine_Dialog routineDialog = new ExerciseRoutine_Dialog();
routineDialog.setTargetFragment(ExerciseRoutine.this, 1);
routineDialog.show(getFragmentManager(), ROUTINE_DIALOG);
}
}

NullPointerException: android.widget.ListView.setAdapter(android.widget.ListAdapter)'

I am trying to show data in drawerLayout by using ListView and customAdapter class but I am facing an error and I am confused to find the solution, why is it showing me NullPointerException?
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nabia.myapplication/com.example.nabia.myapplication.Activities.FindNearbyMosque}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.nabia.myapplication.Activities.FindNearbyMosque.onCreate(FindNearbyMosque.java:94)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5942) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372)
My ListView code is given below.
public class FindNearbyMosque extends AppCompatActivity {
private List<ItemSlideMenu> itemSlideMenus;
private SlidingMenuAdapter slidingMenuAdapter;
private ListView listviewsliding;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Init component
listviewsliding = (ListView) findViewById(R.id.listView);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
itemSlideMenus = new ArrayList<>();
//Add itmes for sliding list
itemSlideMenus.add(new ItemSlideMenu(R.drawable.time, "Masjid Timing"));
itemSlideMenus.add(new ItemSlideMenu(R.drawable.mosque, "My Mosque"));
itemSlideMenus.add(new ItemSlideMenu(R.drawable.settings, "Setting"));
slidingMenuAdapter = new SlidingMenuAdapter(this, itemSlideMenus);
listviewsliding.setAdapter(slidingMenuAdapter);
//Display icon to open/ close sliding list
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Set title
setTitle(itemSlideMenus.get(0).getTitle());
//Item selected
listviewsliding.setItemChecked(0, true);
//Close menu
drawerLayout.closeDrawer(listviewsliding);
//Display fragment 1 when start
replaceFragment(0);
//Handle on item click
listviewsliding.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Set title
setTitle(itemSlideMenus.get(position).getTitle());
//Item selected
listviewsliding.setItemChecked(position, true);
//Replce Fragment
replaceFragment(position);
//close menu
drawerLayout.closeDrawer(listviewsliding);
}
});
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_opened, R.string.drawer_closed){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
//Create method replace fragment
private void replaceFragment (int pos){
Fragment fragment = null;
switch (pos){
case 0:
fragment = new MasjidTiming();
break;
case 1:
fragment = new MyMosque();
break;
case 2:
fragment = new Settings();
break;
default:
fragment = new MasjidTiming();
break;
}
if (null!=fragment)
{
FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_consent, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
Adapter class is:
public class SlidingMenuAdapter extends BaseAdapter {
private Context context;
private List<ItemSlideMenu> Istitem;
public SlidingMenuAdapter(FindNearbyMosque context, List<ItemSlideMenu> istitem) {
this.context = context;
Istitem = istitem;
}
public int getCount() {
return Istitem.size();
}
#Override
public Object getItem(int position) {
return Istitem.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=View.inflate(context, R.layout.itme_sliding_menu,null);
ImageView img = (ImageView)v.findViewById(R.id.item_img);
TextView tv=(TextView)v.findViewById(R.id.item_title);
ItemSlideMenu itemSlideMenu=Istitem.get(position);
img.setImageResource(itemSlideMenu.getImgid());
tv.setText(itemSlideMenu.getTitle());
return v;
}
}
You didn't called setContentView in your activity which makes it impossible to find your ListView.
In your Adapter Class, change this line
View v=View.inflate(context, R.layout.itme_sliding_menu,null);
with
if(convertView == null){
convertView = View.inflate(context, R.layout.itme_sliding_menu,null);
}
And replace return v; with return convertView;
I have fixed my problem by adding setContentView(R.layout.activity_map); line in onCreate method.
Be sure to try cleaning your project. This has solved my problem. I suppose android studio generated bad list of ids which lead to runtime error.

Categories

Resources