How to replace a fragment from onOptionsItemSelected? [duplicate] - java

This question already has answers here:
findViewById in Fragment
(37 answers)
Closed 5 years ago.
I've got an AppCompatActivity which is the main View of the app.
There I've got an OptionsMenu with a button to display a drawer menu and another button to filter the results (Because each MenuItem in the drawer are different Listfragments).
What I need to accomplish is, when clicking the filter button on the OptionsMenu, open a fragment with all the filtering criteria.
So this is the code managing the explained above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_menu:
if (drawer.isDrawerOpen(GravityCompat.START)) {
hideDrawer();
} else {
showDrawer();
}
return true;
case R.id.action_filter:
fragmentManager = getFragmentManager();
fragment = new FilterFragment();
args.putInt("idList", idList);
fragment.setArguments(args);
fragmentManager.beginTransaction().replace(R.id.drawer_layout, fragment).commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Doing that the Fragment shows up and then I get a NPE:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TableRow.setVisibility(int)' on a null object reference
Fragment onCreateViewMethod
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
getActivity().invalidateOptionsMenu();
skbrDays = getActivity().findViewById(R.id.seekBar);
daysLostRow = getActivity().findViewById(R.id.daysLostRow);
genderRow = getActivity().findViewById(R.id.genderRow);
locationRow = getActivity().findViewById(R.id.locationRow);
rgupGender = getActivity().findViewById(R.id.rgupGender);
rgupType = getActivity().findViewById(R.id.rgupType);
btnBreed = getActivity().findViewById(R.id.btnBreed);
recyclerView = getActivity().findViewById(R.id.recyclerView);
daysLostRow.setVisibility(View.VISIBLE);
return inflater.inflate(R.layout.activity_filter, container, false);
}
Ok, so I'm getting the NPE because getActivity() is returning null. And now I have to guess, not really knowing the reason, that is beacuse I'm not replacing the fragment on onCreate method from the activity?
If that is the reason how can I achieve the same result calling the fragment from onCreate?

Replace your code in onCreateView to below code.
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_filter, container, false);
setHasOptionsMenu(true);
getActivity().invalidateOptionsMenu();
skbrDays = rootView.findViewById(R.id.seekBar);
daysLostRow = rootView.findViewById(R.id.daysLostRow);
genderRow = rootView.findViewById(R.id.genderRow);
locationRow = rootView.findViewById(R.id.locationRow);
rgupGender = rootView.findViewById(R.id.rgupGender);
rgupType = rootView.findViewById(R.id.rgupType);
btnBreed = rootView.findViewById(R.id.btnBreed);
recyclerView = rootView.findViewById(R.id.recyclerView);
daysLostRow.setVisibility(View.VISIBLE);
return rootView;
}

Related

App crashes when running code from in fragment

I am making my first app in android studio. So far I have built a bottom navigation bar using fragments. I am now trying to add a calendar feature to one of the fragments, I have copied a tutorial using activities to run the code and think there is a problem with using "View".
This is the code to open for the nav bar to open the fragment.
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:
getSupportFragmentManager().beginTransaction().replace(R.id.container,homeFragment).commit();
return true;
case R.id.calendar:
getSupportFragmentManager().beginTransaction().replace(R.id.container,calendarFragment).commit();
return true;
case R.id.wellbeing:
getSupportFragmentManager().beginTransaction().replace(R.id.container,wellbeingFragment).commit();
return true;
}
return false;
This is java in my calendar fragment, which when opened from the navigation bar crashes the app
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
initWidgets();
selectedDate = LocalDate.now();
setMonthView();
return inflater.inflate(R.layout.fragment_calendar, container, false);
}
private void initWidgets()
{
calendarRecyclerView = requireView().findViewById(R.id.calendarRecyclerView);
monthYearText = requireView().findViewById(R.id.monthYearTV);
}
private void setMonthView()
{
monthYearText.setText(monthYearFromDate(selectedDate));
ArrayList<String> daysInMonth = daysInMonthArray(selectedDate);
CalendarAdapter calendarAdapter = new CalendarAdapter(daysInMonth, this);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(),7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarRecyclerView.setAdapter(calendarAdapter);
}
This is the error message I receive in my logcat
2022-05-12 12:45:05.305 10950-10950/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 10950
java.lang.IllegalStateException: Fragment CalendarFragment{c4de180} (94b1c782-ca39-403f-b45b-5d0d7a042f15 id=0x7f080087) did not return a View from onCreateView() or this was called before onCreateView().
at androidx.fragment.app.Fragment.requireView(Fragment.java:1964)
at com.example.myapplication.CalendarFragment.initWidgets(CalendarFragment.java:41)
at com.example.myapplication.CalendarFragment.onCreateView(CalendarFragment.java:30)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
I believe there is no error in the calendar code and is something to do with onCreateView in the fragment code.
You cannot call requireView() before returning a non-null value from onCreateView().
Move these lines
initWidgets();
selectedDate = LocalDate.now();
setMonthView();
(where initWidgets() calls requireView()) from onCreateView() to e.g. onViewCreated().
Yes it crashes because findViewById wasn't called from the fragment_calendar layout before returning inflater therefore anything called after the return statement will not be executed and will return a NullPointerException. Unless you override onViewCreated method and put down the codes
So to avoid that, you need to override onViewCreated method or you must return view. Remember, if you want to return view then all codes or public methods must be reached before the return view statement. Else, it'll crash due to exception
Check this codes, it'll fix it correctly
public class TestFragment extends Fragment {
RecyclerView calendarRecyclerView;
TextView monthYearText;
#Nullable
#org.jetbrains.annotations.Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
selectedDate = LocalDate.now();
return inflater.inflate(R.layout.fragment_calendar, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initWidgets(view);
setMonthView();
}
private void initWidgets(View view) {
calendarRecyclerView = view.findViewById(R.id.calendarRecyclerView);
monthYearText = view.findViewById(R.id.monthYearTV);
}
private void setMonthView() {
monthYearText.setText(monthYearFromDate(selectedDate));
ArrayList<String> daysInMonth = daysInMonthArray(selectedDate);
CalendarAdapter calendarAdapter = new CalendarAdapter(daysInMonth, getActivity());
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarAdapter.setAdapter(calendarAdapter);
}
}

nullPointerException between fragment and main activity, android/android-studio [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I want to make a communication between my fragment and main activity so I need to get switch.isCheked but get:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new NotesFragment()).commit();
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
final NotesFragment notesFragment = new NotesFragment();
final RemindersFragment remindersFragment = new RemindersFragment();
final SettingsFragment settingsFragment = new SettingsFragment();
final Switch aSwitch = settingsFragment.getView().findViewById(R.id.switch_pop_up);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.action_notes:
if(aSwitch.isChecked())
Toast.makeText(MainActivity.this, "Your Notes", Toast.LENGTH_SHORT).show();
selectedFragment = notesFragment;
break;
case R.id.action_reminders:
if(aSwitch.isChecked())
Toast.makeText(MainActivity.this, "Your Reminders", Toast.LENGTH_SHORT).show();
selectedFragment = remindersFragment;
break;
case R.id.action_settings:
if(aSwitch.isChecked())
Toast.makeText(MainActivity.this, "Settings", Toast.LENGTH_SHORT).show();
selectedFragment = settingsFragment;
break;
}
assert selectedFragment != null;
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
});
}
}
SettingsFragment.java
public class SettingsFragment extends Fragment {
Switch switch_pop_up;
boolean stateSwitchPopUp;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_settings, null);
switch_pop_up = v.findViewById(R.id.switch_pop_up);
return inflater.inflate(R.layout.fragment_settings,container,false);
}
}
I think it is because of :
final Switch aSwitch = settingsFragment.getView().findViewById(R.id.switch_pop_up);
How can I fix this?
The problems are:
you are not attaching your SettingsFragment to activity to make it visible on screen (like you do with your NotesFragment)
you're trying to access it's child views from your MainActivity's onCreate method.
Fragments get initialized after activity is being created and ready to host them. Therefore, settingsFragment.getView() is returning null. It's onCreateView lifecycle method has not being called yet.
You need to reconsider your approach and try to find other solution. I suggest you to read this threads in SO to get more ideas on how to solve your problem.
How to access Fragment's child views inside fragment's parent Activity?
Access Fragment View from Activity's onCreate
Android: how to notify Activity when Fragments views are ready?
On your fragment, in the onCreateView method, change this line:
View v = inflater.inflate(R.layout.fragment_settings, null);
into:
View v = inflater.inflate(R.layout.fragment_settings, container, false);
Edit 1:
This happens because you're doing v.findViewById(), but you're not returning v on onCreateView().
Change your return line in the fragment to return v;
Edit 2:
Add a getter on the fragment to the switch_pop_up:
public Switch getSwitchPopUp() {
return switch_pop_up;
}
On your activity, replace this line:
final Switch aSwitch = settingsFragment.getView().findViewById(R.id.switch_pop_up);
with:
final Switch aSwitch = settingsFragment.getSwitchPopUp();
And you'll have to inflate your SettingsFragment before this line.
Note: Whichever result you need, this is not a good aproach anyway, so you might want to look into other ways to do it.

ListView of a Fragment (of a ViewPager inside a Fragment) that implements LoaderCallbacks<Cursor> is empty when I return back to it

The ListView inside of a Fragment that contains a ViewPager is empty when I return back to it. This ViewPager is inside of a Fragment because I'm using a Navigation Drawer Layout. I'll try to explain it in more details now:
I am using a Navigation Drawer layout and, consequently, using Fragments. In the main activity, in onCreate, I set the current Fragment with these lines of code:
#Override
protected void onCreate(Bundle savedInstanceState) {
/* More code */
currentFragment = new MainFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.layout_for_fragments, currentFragment).commit();
}
The MainFragment class contains a ViewPager that is initialized with these lines of code in the onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
/* More code */
ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.main_view_pager);
viewPager.setAdapter(new SimpleFragmentPagerAdapter(getActivity(), getActivity().getSupportFragmentManager()));
/* Code for Tablayout */
return rootView;
}
One of the Fragments of the SimpleFragmentPagerAdapter class is this one that implements LoaderManager.LoaderCallbacks
public class ExpensesFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private ExpenseCursorAdapter mExpenseCursorAdapter;
private static final int EXPENSE_LOADER = 1;
public ExpensesFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_expenses, container, false);
ListView expensesListView = (ListView) rootView.findViewById(R.id.expenses_list_view);
mExpenseCursorAdapter = new ExpenseCursorAdapter(getContext(), null);
expensesListView.setAdapter(mExpenseCursorAdapter);
getLoaderManager().initLoader(EXPENSE_LOADER, null, this);
return rootView;
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(/* arguments */);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mExpenseCursorAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
mExpenseCursorAdapter.swapCursor(null);
}
}
On the first run, everything works fine. However, if I click on the navigation drawer menu, which I'm implementing this way, the ListView becomes empty:
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_main) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().remove(currentFragment).commit();
currentFragment = new MainFragment();
fragmentManager.beginTransaction().replace(R.id.layout_for_fragments, currentFragment).commit();
} else if (id == R.id.nav_blank) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().remove(currentFragment).commit();
currentFragment = new BlankFragment();
fragmentManager.beginTransaction().replace(R.id.layout_for_fragments, currentFragment).commit();
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
The problem also exists when I rotate the device. There is a difference though. When I use the Navigation Drawer menu, none of the methods of the LoaderManager.LoaderCallbacks are called, but when I rotate the device, onLoadFinished is called.
Anyway, the problem persists. Any ideias how to solve it?
Thanks in advance.
I have found the solution!
Basically, I made two mistakes.
The first one is about the rotation of the device.
When the device is rotated, the Activity is destroyed and onCreate is called again. In this case, I should't call new MainFragment(). So, I check if the savedInstanceState is null before I call it, like this:
if (savedInstanceState == null) {
currentFragment = new MainFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().
replace(R.id.layout_for_fragments, currentFragment).commit();
}
The second one is about a fragment within a fragment. In this case, I can't simply call getSupportFragmentManager(). I should call getChildFragmentManager() instead and that's it!

Android - Accessing views in a fragment

I have a navigation drawer activity where i am attaching different fragments
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, rp_fragment.OnFragmentInteractionListener, sensors_fragment.OnFragmentInteractionListener, stats_fragment.OnFragmentInteractionListener {
I'm trying to modify the text property of a textview contained within a fragment but i keep getting a null pointer exception on the view (java.lang.NullPointerException: Attempt to invoke virtual method 'void com.teicrete.alucard.sempi.sensors_fragment.ctv_tvst(int)' on a null object reference).
Mainactivity:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (id == R.id.nav_camera) {
ft.replace(R.id.fragment_placeholder, new rp_fragment());
ft.commit();
} else if (id == R.id.nav_gallery) {
ft.replace(R.id.fragment_placeholder, new sensors_fragment());
ft.commit();
gentempdata();
sensors_fragment sf = (sensors_fragment)fm.findFragmentById(R.id.fragment_sensors);
sf.ctv_tvst(1);
Fragment:
public class sensors_fragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
TextView ctv_tv;
TextView otv_tv;
View view;
...
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sensors, container, false);
TextView ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
TextView otv_tv = (TextView)view.findViewById(R.id.otv_tv);
//ctv_tv.setText("Test"); Only this works
return view;
}
...
public void ctv_tvst(int mtext) {
ctv_tv.setText(mtext);
}
I've looked at similar posts here but i was not able to resolve my issue. The only place where i am able to modify the textview is within the onCreateView of the fragment (see code above). If i try to do it from the mainactivity or anywhere else i get the null pointer error.
Any insights?
Edit: Please see my response in android_griezmann's post.
For now, i am doing everything that i need to do, inside the fragment classes themselves and then i load them. I would still like to figure out why i can't access its methods or views externally.
Change those lines from onCreateView:
TextView ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
TextView otv_tv = (TextView)view.findViewById(R.id.otv_tv);
To:
ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
otv_tv = (TextView)view.findViewById(R.id.otv_tv);
Currently you are assigning reference to new object by TextView ctv_tv but actually you want this reference to be assigned to class field so you don't have to add TextView.
Your problem is Fragment itself not the TextView. You are getting fragment object as null not the textview.
So try to find the fragment like the below.
if (id == R.id.nav_gallery) {
ft.replace(R.id.fragment_placeholder, new sensors_fragment(),"YOUR_TAG_NAME");
ft.commit();
gentempdata();
sensors_fragment sf = (sensors_fragment) fm.findFragmentByTag("YOUR_TAG_NAME");
sf.ctv_tvst(1);
}
Also change this method too!
public void ctv_tvst(int mtext) {
ctv_tv.setText("" + mtext);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView ctv_tv = (TextView)getView().findViewById(R.id.ctv_tv);
TextView otv_tv = (TextView)getView().findViewById(R.id.otv_tv);
}
override onactivity created, and then refer your views.. Infact it is the best place to get reference to your views withot getting NullPointer Exception
If you want to pass data while replacing or adding then pass the data in your fragment constructor like below
else if (id == R.id.nav_gallery) {
ft.replace(R.id.fragment_placeholder, new sensors_fragment("your data"));
ft.commit();
Then in your fragmet change the textview value based on the constructor's data
Here is the problem. Just remove the TextView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sensors, container, false);
**TextView** ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
**TextView** otv_tv = (TextView)view.findViewById(R.id.otv_tv);
//ctv_tv.setText("Test"); Only this works
return view;
}
Replace the following code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sensors,container, false);
TextView ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
TextView otv_tv = (TextView)view.findViewById(R.id.otv_tv);
//ctv_tv.setText("Test"); Only this works
return view;
}
with
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sensors,container, false);
ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
otv_tv = (TextView)view.findViewById(R.id.otv_tv);
//ctv_tv.setText("Test"); Only this works
return view;
}
Create a variable String ctv_tv_string and assign the value in ctv_tvst() method do not assign to the TextView. it would not throw an error as you are not referring any ui element after this in oncreateview ctv_tv.setText(ctv_tv_string)

Android Studio - Fragment button action

I tried to make an application that shows a fragment at start then you can change that fragment to an other one with a button in the first fragment. But when I put the button action in the fragments java, it won't start and I get the nullpointerexception error. Could you tell me why?
public class Fragment_main extends Fragment {
FragmentManager fm = getFragmentManager();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Button button_inditas = (Button) getView().findViewById(R.id.button_inditas);
button_inditas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fm.beginTransaction().replace(R.id.content_frame, new Fragment_1()).commit();
}
});
return inflater.inflate(R.layout.fragment_main,container,false);
}
}
But when I put the button action in the fragments java, it won't start and I get the nullpointerexception error. Could you tell me why?
Well I see some errors on your code...
First of all you can't call getView() if you haven't inflated one, it means that you MUST inflate a view as i'll put on my answer and then you can avoid the getView() and use that view itself.
And instead of returning the inflater you have to return your View
This is how your Fragment should look like:
public class Fragment_main extends Fragment {
public Fragment_main() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main,container,false);
Button button_inditas = (Button)rootView.findViewById(R.id.button_inditas);
button_inditas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager ();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();
fragmentTransaction.add (R.id.content_frame, new Fragment_1());
fragmentTransaction.commit ();
});
return rootView;
}
}
You have to inflate the view first and use the view returned by that instead of getView ().
View view = inflater.inflate (...);
and then
... = (Button) view.findView(...);
This happens because the view that is returned by getView () hasn't been created yet, so getView() returns null.

Categories