I have a fragment and I am inflating it like below, but it is giving runtime error:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View jobsView = inflater.inflate(R.layout.fragment_job_information, container, false);
textView = (TextView) jobsView.findViewById(R.id.textView);
if (jobInfo == null) {
EmployerHistory jobsQuery=new EmployerHistory();
jobInfo = jobsQuery.getJobInfo(employerName, position);
}
textView.setText(jobInfo.get("jobName").toString());
return jobsView;
}
Its corresponding layout is:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/jobInfoFragment">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ankit.job_depot.employer.view.JobInformation">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</fragment>
Actually I am already on a fragment with listview and when user clicks on an element of listview that fragment needs to get changed by a new fragment, in my new fragment I have:
l
istViewJobHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
JobInformation newFragment = new JobInformation();
android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.listViewJobHistory, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
I am getting the following exception:
07-28 18:49:39.681 14474-14474/com.example.ankit.job_depot
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ankit.job_depot, PID: 14474
android.view.InflateException: Binary XML file line #1: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.example.ankit.job_depot.employer.view.JobInformation.onCreateView(JobInformation.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
I think your main Activity should extend FragmentActivity
import android.support.v4.app.FragmentActivity;
Related
The crash is when i switch the page from 2 to 0 or 1.
And when i Comment the auto Complete Fragment at activity_maps.xml
Please help me.
--------- beginning of crash
12-04 01:31:01.005 12693-12693/com.handsomelee.gotroute E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.handsomelee.gotroute, PID: 12693
android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #10: Duplicate id 0x7f0f00e4, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2315)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:5901)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.handsomelee.gotroute.Services.GoogleMapSystem.onCreateView(GoogleMapSystem.java:41)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2261)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:792)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2209)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:649)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1238)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:663)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:625)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:617)
at com.handsomelee.gotroute.MainActivity$1.onTabSelected(MainActivity.java:73)
at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1165)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1158)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1128)
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1427)
at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1537)
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)
--------- beginning of system
this is my MainActivity.java
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
if(tab1 == null){
tab1 = new MapsActivity(R.id.mapView, R.layout.activity_maps, GoogleMap.MAP_TYPE_NORMAL);
}
return tab1;
case 1:
if(tab2 == null)
tab2 = new CarParkingActivity();
return tab2;
case 2:
if(tab3 == null)
tab3 = new ReportActivity();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return 3;
}
MapsActivity.java
#Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
rootView = layoutInflater.inflate(layoutActivityId, viewGroup, false);
mapView = ((MapView) rootView.findViewById(mapViewId));
mapView.onCreate(bundle);
mapView.getMapAsync(this);
addOn();
return rootView;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(googleMapType);
mMap.setOnMyLocationButtonClickListener(this);
mMap.setOnMyLocationClickListener(this);
mMap.setOnMapLongClickListener(this);
}
}
activity_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.handsomelee.gotroute.Controller.ReportActivity">
<fragment
android:layout_width="0dp"
android:layout_height="50dp"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:id="#+id/placeSearch"
android:layout_weight="0.67"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<com.google.android.gms.maps.MapView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/mapView"
android:layout_weight="0.78"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:apiKey="#string/test_google_api"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintTop_toBottomOf="#+id/placeSearch"/>
</android.support.constraint.ConstraintLayout>
ReportActivity.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_report, container, false);
spinner = (Spinner) rootView.findViewById(R.id.spinner);
String[] array_Spiner = getResources().getStringArray(R.array.reports_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(rootView.getContext(), R.layout.support_simple_spinner_dropdown_item, array_Spiner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
~
});
return rootView;
}
activity_report.xml
<?xml version="1.0" encoding="utf-8"?>`
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.handsomelee.gotroute.Controller.ReportActivity">
<Spinner
android:layout_width="0dp"
android:layout_height="50dp"
android:id="#+id/spinner"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:overlapAnchor="false"
android:dropDownWidth="match_parent"
app:layout_constraintHorizontal_bias="0.0"/>
</android.support.constraint.ConstraintLayout>
SOLVED.
The problem seems like is the fragment id duplicated when the view is created.
so I add the Code below at my MapActivity to remove it.
#Override
public void onDestroyView() {
super.onDestroyView();
android.app.FragmentManager fm = getActivity().getFragmentManager();
android.app.Fragment fragment = fm.findFragmentById(R.id.placeSearch);
android.app.FragmentTransaction ft = fm.beginTransaction();
ft.remove(fragment);
ft.commit();
}
Can somebody explain what is the problem here? The error is bellow:
Code in MainAcitivity:
FragmentManager fm = getSupportFragmentManager();
SvePonudeFragment fragment = (SvePonudeFragment) fm.findFragmentById(R.id.ponudice);
fragment.reloadData();
Fragment Class:
public class SvePonudeFragment extends Fragment {
private RecyclerView rv;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sve_ponude_fragment, container, false);
rv = (RecyclerView) rootView.findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
ArrayList<Ponuda> listaPonuda = null;
initializeAdapter(listaPonuda);
return rootView;
}
private void initializeAdapter(List<Ponuda> preuzetePonude){
RVAdapter adapter = new RVAdapter(preuzetePonude);
rv.setAdapter(adapter);
}
public void reloadData(){
System.out.println("I ENTEEERED");
}
}
XML of fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/ponudice">
<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/rv"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
What is wrong with this? Can somebody explain me?
this is the error:
W/System.err: java.lang.NullPointerException
W/System.err: at com.example.filip.dajsve.Activities.MainActivity.onDataLoaded(MainActivity.java:190)
W/System.err: at com.example.filip.dajsve.Loaders.DatabaseDataLoader.loadData(DatabaseDataLoader.java:22)
I found the problem. The problem is that you don't link your fragment with mainActivity. first you should add your fragment in xml of mainActivty and give the fragment an id then access it in the main activity with the id of fragment not using this id android:id="#+id/ponudice"> because this id related with relative layout not the fragment. check the xml code of main activity you should find fragment tags like code below
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="theNameOfYourPackage.theNameOfFragment"
android:id="#+id/ponudice_in_main_activity"/>
Do it like this
FragmentManager fm = getSupportFragmentManager();
SvePonudeFragment fragment = new SvePonudeFragment();
fm.beginTransaction().replace(R.id.framecontainer,fragment).commit();
and your activity_main.xml file will be
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/framecontainer"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="16dp"
>
</FrameLayout>
I have created 2 fragment and their layout,
when i click button on profile fragment it should open flat layout, but
it force stop application and its not opening the second fragment,
i have included fragment code and its layout code..
profilefragment.java
public class ProfileFragment extends Fragment {
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.profile, container, false);
Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
onButtonClicked(v);
}
});
return rootView;
}
public void onButtonClicked(View view)
{
//do your stuff here..
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.button, new flatview(), "NewFragmentTag");
ft.commit();
}
profile.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}"
android:id="#+id/semester">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
flatview.java
public class flatview extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.flat, container, false);
}
flat.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="second fragement"
android:id="#+id/textView3"
android:layout_gravity="center_horizontal" />
logcat
06-19 07:57:56.945 26457-26457/com.idealdeveloper.saltechnical E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.idealdeveloper.saltechnical, PID: 26457
java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.ViewGroup
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Try this I guess it will work for you
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}"
android:id="#+id/semester">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/fragmentLayout"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
Class
public class ProfileFragment extends Fragment {
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.profile, container, false);
Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
onButtonClicked(v);
}
});
return rootView;
}
public void onButtonClicked(View view)
{
//do your stuff here..
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragmentLayout, new flatview(), "NewFragmentTag");
ft.commit();
}
I hope this one will helps you
The fragment transaction inserts a View into a ViewGroup, so you can't just replace a Button. You could just replace the parent semester, since it is a ViewGroup.
public void onButtonClicked(View view) {
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.semester, new flatview(), "NewFragmentTag");
ft.commit();
}
If your intention is also to remove the button, you will need to do it manually since the FragmentManager can only modify runtime transactions:
ViewGroup parent = (ViewGroup) getActivity().findViewById(R.id.semester);
parent.removeView(view);
You have to provide a ViewGroup root for the flat.xml layout , for example you can surround the TextView with a LinearLayout
flat.xml :
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="second fragement"
android:id="#+id/textView3"
android:layout_gravity="center_horizontal" />
</LinearLayout>
I have several fragments. Some fragments have the same buttons at the top and the bottom of the fragment, but the content is different.
Therefore I'm using nested fragments for the top and the buttom part. This is my java class: LogFragment.java. I insert two fragments for the top navigation and the footer at the bottom.
public class LogFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootview = inflater.inflate(R.layout.fragment_log, container, false);
Fragment navigationFrag = new NavigationFrag();
FragmentTransaction trans1 = getChildFragmentManager().beginTransaction();
trans1.add(R.id.navigationFrag, navigationFrag).commit();
Fragment footerFragment = new FooterFragment();
FragmentTransaction trans2 = getChildFragmentManager().beginTransaction();
trans2.add(R.id.footerFragment, footerFragment).commit();
return rootview;
}
}
This is the layout file: fragment_log.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout android:id="#+id/NavigationFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_below="#+id/button1"/>
</RelativeLayout>
<FrameLayout android:id="#+id/FooterFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The nested NavigationFragment and FooterFragment contains some buttons, images or textfields. For example: fragment_footer.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/logInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOG" />
</RelativeLayout>
My question:
How can I access/manipulate/fill in some data of the elements in my nested fragments by the LogFragment.java class?
For example:
I want to access the textview with the id logInfo of the nested fragment_footer.xmlby the LogFragment.java.
Just use [child fragment].getView().findViewById(R.id.[elemnet id]) to get the elements in nested fragments. For example :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootview = inflater.inflate(R.layout.fragment_log, container, false);
Fragment navigationFrag = new NavigationFrag();
FragmentTransaction trans1 = getChildFragmentManager().beginTransaction();
trans1.add(R.id.navigationFrag, navigationFrag).commit();
final Fragment footerFragment = new FooterFragment();
FragmentTransaction trans2 = getChildFragmentManager().beginTransaction();
trans2.add(R.id.footerFragment, footerFragment).commit();
Button btn1 = (Button)rootview.findViewById(R.id.button1);
btn1.setOnClickListener( new OnClickListener(){
#Override
public void onClick(View v) {
TextView txv = (TextView)footerFragment.getView().findViewById(R.id.logInfo);
txv.setText("Updated log of Footer");
}
});
return rootview;
}
By the way,
the child fragment is available to access only after onCreateView(),
so you need to declare the child fragment as final or member data of parent.
so I know there are a few topics around here that ask about this but none seemed to be the cause of my issue. I have an ActionBarActivity that is going to be switching between a few different fragments, but I'm having a problem getting one of the fragments to show up. The activity does show a fragment when it is first loaded and does that fine. Here is the code that is setting up the initial fragment with the method to switch between fragments that I'm using.
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
final ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("");
if (savedInstanceState == null) navigateTo(START_SCREEN);
}
public void navigateTo(int sectionNumber, Serializable objectParam) {
Fragment fragment = null;
switch (sectionNumber) {
case START_SCREEN:
fragment = createStartScreenFragment(); //returns an instance of the class for this fragment
break;
case SCREEN_TWO:
fragment = createSecondScreenFragment(); //returns an instance of the class for this fragment
break;
}
if (fragment == null) return;
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction trans = fm.beginTransaction();
trans.add(R.id.activity_container, fragment, fragment.getTag());
trans.commit();
trans.show(fragment);
}
activity_containers.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:id="#+id/activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
The first fragment that loads successfuly
<GridLayout
android:id="#+id/fragment_start_screen"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android.Activity">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
/>
...
</GridLayout>
And here is the fragment xml that isn't showing up
<TextView android:id="#+id/section_label" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
android:padding="2dp">
</ListView>
And here is the misbehaving fragments relevant code:
public class SecondFragment extends Fragment {
public final static int SECTION_NUMBER = 1;
private String mTag = "TAG";
private ParentActivity parentActivity;
public SecondFragment() {
}
public static fragment newInstance() {
SecondFragment fragment = new SecondFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onAttach(final Activity activity) {
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
parentActivity = (ParentActivity) getActivity();
View rootView = inflater.inflate(R.layout.fragment_second_fragment, container, false);
ListView list = (ListView) rootView.findViewById(R.id.list);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
The biggest problem I'm having here is that there is no error message shown. The text view in the first fragment is supposed to trigger the second fragment to show up on the screen. This used to work when I had that fragment as a DialogFragment and just called .show() on an instance of it. I need it to be a regular fragment now and it doesn't show up. There's no error message that I can see, though debugging it does show it running through all of my code successfully and it looks like its properly inflating the view.
So what could I be missing here that is preventing the fragment from displaying?
Your layout
<TextView android:id="#+id/section_label" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
android:padding="2dp">
</ListView>
Should be within a ViewGroup
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:id="#+id/section_label" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
android:padding="2dp">
</ListView>
</LinearLayout>
And you should return rootView in onCreateView().
Also, you might need to use replace method of Fragment Manager to replace one fragment with another, instead of add.