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.
Related
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);
I've got a problem. For school project I have to create app in Android Studio with few activities so i decided to add Navigation Drawer, but when i try to create simple calculator to app i can't even reference button or textview. This is code for activity where I want to create calculator. Also I have to add Map Activity and SQLite Database.
public class Calculator_main extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.calculator, container, false);
return rootview;
}
}
Use rootView to find your item when referencing in a Fragment:
public class Calculator_main extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.calculator, container, false);
//Notice the TextView being referenced with rootView in front of the findViewById statement
TextView namefield = (TextView) rootView.findViewById(R.id.namefield);
return rootview;
}
}
You need to do this when using Fragment, as this is the only way your app can reference an item in your Fragment's XML view
This is however not necessary in an Activity and should not be confused with the method of referencing in a Fragment
I'm new to android programming. I want to write an android program which contains two fragments. Designs' will be same but I can't write layout file twice as it has a lot of widgets so is there a way to handle this?
If I copy xml file widget ids' stay same and I can't reach them in my java class...
Lets assume you have fragment_container.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fraContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
What you need to do is to replace this container with one of the fragments in your program like this for example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_container);
if (findViewById(R.id.fraContainer) != null)
{
MyFragmentA myFragment = new MyFragmentA();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fraContainer, myFragment)
.commit();
}
}
This will replace the fragment layout (container) with the fragment you want.
Now lets examine MyFragmentA, and MyFragmentB
public class MyFragmentA extends Fragment {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_both, container, false);
TextView x = (TextView)view.findViewById(R.id.textView);
x.setText("I am fragment A");
return view;
}
...
And the second fragment
public class MyFragmentB extends Fragment {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_both, container, false);
TextView x = (TextView)view.findViewById(R.id.textView);
x.setText("I am fragment B");
return view;
}
...
Both inflate the same fragment_both.xml, and both use the same TextView with the same ID, by referring to the view object!
I hope that helps you understand it.
You can simply use the same xml file in more fragments, like this:
Fragment A
public class FragmentA extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.your_layout, container, false);
...
return v;
}
}
Fragment B
public class FragmentB extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.your_layout, container, false);
...
return v;
}
}
my name is Augusto, I'm developing a Messaging/Social app for Android on Android Studio and I implemented SlidingTabLayout (com.google.samples.apps.iosched.ui.widget) with 2 tabs (Chats and Friends ).
I wish to know how to access the views inside the Tabs' xml files on my activity.
I have:
MainActivity.class with activity_main.xml as contentview and the tabs's layouts(friendsview.xml and chatsview.xml) for the tabs.
I want to access tabs' layouts view (e.g: EditText, ImageView) from MainActivity.class, How do I "import" them to the activity?
Thanks!
SlidingTabLayout it's only a layout. To manage content use fragments with FragmentPagerAdapter for your tabs.
Here (link) is nice tutorial for you.
If you follow this example tutorial, you will have
public class Tab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
return v;
}
}
and to access EditText and other widget inside tab, you need to have reference from view, so change above code to:
public class Tab1 extends Fragment {
private EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.chatsview,container,false);
editText = v.findViewById(R.id.YOUR_EDIT_TEXT_ID);
return v;
}
}
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