Java – Android FragmentLayout is not loading

Android FragmentLayout is not loading… here is a solution to the problem.

Android FragmentLayout is not loading

I’m new to Android development and may need your help.
I’m using a FrameLayout with fragments for navigation. The XML files are correct (or rather, they appear correctly in the IDE).
But loading fragments doesn’t work. And without any errors can help me.

The only error is that every time the fragment is loaded, the debugger says:

W/PathParser: Points are too far apart 4.000000596046461

XML

is very basic XML (template with HelloWorld-Textbox) provided by Android Studio. I’m loading fragment :

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new Dashboard());
fragmentTransaction.commit();

R.id.fragment_container is correct (first I don’t have that much stuff that I might confuse, and secondly I checked twice).

Thank you very much for your time, hope it helps me. If you need any further information, please ask from them.

Add to:
Fragment (change as soon as it takes effect):

<FrameLayout 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.david_haintz.virtualdatingcoach.Community">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

</FrameLayout>

Main content:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.david_haintz.virtualdatingcoach.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></FrameLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.david_haintz.virtualdatingcoach.MainActivity"
tools:showIn="@layout/app_bar_main">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
</RelativeLayout>

Solution

This is really a common problem with images in Android.

Sometimes, when we write a layout file, we want to preview the result, and if the layout has an ImageView, then we also want to view the image by providing image resources in xml. Note that when you set up an image asset in ImageView for preview purposes only, don’t forget to delete it. If we forget to delete it, then at runtime the image will be loaded and sucked up the application memory.

It seems fine if the image is loaded only once, but what if the example image is on the project layout and we use the layout in the Recycler View (ListView)? A sample image is loaded into each item in the list. Just multiply it by the size of the image, which is how an app becomes a memory suction cup.
I made this mistake and luckily I used a very large image, so always throwing an exception.

Here is an example of a preset image resource

 <ImageView
  android:id="@+id/iv_preview"
  android:layout_width="match_parent"
  android:layout_height="200dp"
  android:layout_below="@+id/tv_title"
  android:adjustViewBounds="true"
  android:scaleType="centerCrop"
  android:src="@drawable/budget_preview"
/>

budget_preview.png is a very large picture. For the default activity, my app only uses about 20MB of memory.
enter image description here
But after opening the activity with only 3 projects, it jumped to 90mb, scrolled a little, and the OOM improved.
enter image description here
So don’t forget to remove android:src after finishing previewing, you may leave a good memory.

Hope it works.

Using tools:src instead of android:src to get preview images in Android Studio has zero runtime impact http://tools.android.com/tips/layout-designtime-attributes

Resource links:

Beware of Setting Image Resource for Preview in XML for Android

Related Problems and Solutions