Init FragmentActivity with a single fragment programmatically - java

I'm tring to add a fragment to an activity in order to avoid declaring the layout xml. I'm doing
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new mFragment()).commit();
}
But it crashes with:
java.lang.IllegalStateException: The specified child already has a parent.
You must call removeView() on the child's parent first.
Is there a short form for avoiding this error or I must recover the content layout before and remove all their views? Thanks
Edit mFragment Code:
public class mFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View result= inflater.inflate(R.layout.activity_main, container);
return result;
}
}

Try the following code while inflating.
View result= inflater.inflate(R.layout.activity_main, container, false);
This will avoid adding the view to parent by default.
By using the other method, it will add a parent by default. So when you try to add it dynamically it will throw the above mentioned exception.

Change:
View result= inflater.inflate(R.layout.activity_main, container);
To:
View result= inflater.inflate(R.layout.activity_main, container, false);

Related

Cannot retrieve View in onResume() method inside Fragment

I have a method in my fragment that I would like to call from onResume(). Unfortunately, my method requires that I pass it a View, which is defined in onCreateView():
onResume():
#Override
public void onResume() {
super.onResume();
retrieveData(0, false, rootView);
}
onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Set view
View rootView = inflater.inflate(R.layout.tab_fragment_1, container, false);
...
}
Is there a way to call my retrieveData method as is in onResume(), or should I just create a new method without the rootView parameter (not ideal)?
Can't move it to global scope because the inflater method parameter is part of the fragment interface.
You can only get the view from your fragment, not your activity. To do this, in your fragment onCreateView
save the root view as an Instance Variable like
private View root;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_details_buddy, container, false);
... }
And call it in your onResume
Edit : to get data from Activity, pass it in a Bundle. Doc

Unfortunately, app has stopped,how can I make this work onCreateView?

Previously, I had a similar issue, app crashes when trying to access (or findByID), it turned out, I've put the textview in the wrong method, Now I have only one method, and I am working with a fragment, the method onCreateView and my app keeps crashing:
public class InfoFragment extends Fragment {
public InfoFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView n = (TextView) getView().findViewById(R.id.nama);
n.setText("Some String");
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_info, container, false);
}
}
Originally, I had the line looking like this :
TextView n = (TextView) findViewById(R.id.nama);
However,the compiler gave me an error saying it can't find the method findViewById, so some research showed that I should add getView() , I did, and the error disappeared, but now the app is crashing, any ideas?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_info, container, false);
TextView n = (TextView) view.findViewById(R.id.nama);
n.setText("Some String");
return view;
}
You don't have a view yet in that part of code. Inflate it first and use after.
Inflate your layout first, then you can use it to find the TextView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_info, container, false);
TextView n = (TextView) rootView.findViewById(R.id.nama);
n.setText("Some String");
return rootView;
}

loading GestureLibrary inside Fragment Activity not working

how to load Gesturelibrary inside Fragment Activity ...Please help me
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_a, container, false);
//error is here..."this" is not working
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
gestureOverlayView = (GestureOverlayView)view.findViewById(R.id.gestureOverlayView);
return view;
}
It may be asking for context and it's throwing error because fragment doesn't have context.
Try to use like this:
mLibrary = GestureLibraries.fromRawResource(getActivity(), R.raw.gestures);

FragmentStatePagerAdapter NullException

I have two fragments that contains ViewPager. The first fragment that contain viewPager works very well. As I come to second fragment that contain ViewPager, I start to get FragmentStatePagerAdapter NullException. I knew what is the problem, I need to implement onDetach function to get rid this problem. I search around the internet, there is a lots of solutions. And I'm currently using API 19. So maybe this solution very suitable for me. But I don't know how to use the solution.
getSupportFragmentManager().beginTransaction().detach(mFragment1).replace(R.id.main, mFragment2).attach(mFragment2).addToBackStack(null).commit();
What is exactly mFragment1 and mFragment2 and R.id.main ?
Fragment 1
public class fragment1 extends fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_restaurantprocess, container, false);
adapter = new RestaurantProcessPageAdapter(getActivity().getSupportFragmentManager());
...
}
}
Fragment 2
public class fragment2 extends fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_feedpage, container, false);
adapter = new RestaurantFeedPageFragment(getActivity().getSupportFragmentManager());
...
}
}
I'm using slide animation to fragment
public void changeFragment_toRight(Fragment frag) /* Slide to right screen */ {
if (getSupportFragmentManager().findFragmentByTag("frag")!=null)
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.right_slide_in, R.anim.right_slide_out).replace(R.id.container, frag, "frag").commit();
else
getSupportFragmentManager().beginTransaction().add(R.id.container, frag, "frag").commit();
}
changeFragment_toRight(new fragment1());
changeFragment_toRight(new fragment2());
Source: Click here to view the source
It looks like the problem is that you are manually using a FragmentTransaction to change fragments. A ViewPager will do this automatically when you supply it with an adapter, so when you change fragments, you are trying to remove a fragment that has already been removed.

Android, Java: Bundles & Views for Fragments

I have a fragment, that i want to put a textview which will have a 3 strings stored in it from the 'Main Activity'
The strings that I need to pass will come from the Main Activity and will be passed into FragmentClass
The issue is that its not recognising findViewById, i understand this is something to do with the views? Could someone advise me on how i could get this editText working and then how i can pass a bundle of strings from the MainActivity to the FragmentClass.
#SuppressLint("NewApi")
public class FragmentClass extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.new_fragment, container, false);
// View rootView = inflater.inflate(R.layout.get_data, container, false);
TextView FragmentTextView = (TextView)findViewById(R.id.FragmentTextView);
}
}
Change to
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.new_fragment, container, false);
TextView FragmentTextView = (TextView)view.findViewById(R.id.FragmentTextView);
return view;
}
Also you have #SuppressLint("NewApi") you have suppressed lint warning. Probably indicates that some features are available in new api's only. Do check your min sdk version in manifest

Categories