David
David

Reputation: 388

Android FragmentLayout isn't loaded

I'm a beginning android develope and could need your help. I'm doing a navigation by using a FrameLayout with Fragments. XML-Files are correct (or better to say, they are displayed correct in the IDE). But loading the Fragments doesn't work. And there is no error which helps me.

The only error is everytime a Fragment is loaded, the debugger says:

W/PathParser: Points are too far apart 4.000000596046461

The XML is the very basic XML given bei Android Studio to start (the template with the HelloWorld-Textbox). I'm loading the Fragment with following code:

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

R.id.fragment_container is the correct one (firstly I don't have that much that I could be confused and secondly I checked it twice).

Many thanks for your time and I hope there is help for me. If you need any further information, please request them.

Adds: Fragment (will be changed as soon as it works):

<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:

<?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>

Upvotes: 3

Views: 1417

Answers (2)

ballu
ballu

Reputation: 49

If your using first time in your activity use this

fragmentTransaction.add(R.id.fragment_container, new Dashboard());

in the place of

fragmentTransaction.replace(R.id.fragment_container, new Dashboard());  

suppose if you have more then one fragment then you will use inside if (or) switch,case that replace fragment.
I think you solve the problem when changing the replace with add

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

use this code

Upvotes: 2

SkyWalker
SkyWalker

Reputation: 29140

It’s really common issue with image 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 see the image by just giving the image resources in xml. Beware of that, by the time you set image resource in image view for only preview purpose, don’t forget to remove it. If we forget to remove it, then in the runtime the image will be loaded and suck the app memory.

It would seem fine if the image is only loaded once but what if the sample image is on item layout, and we use the layout in recycler view (listview)? the sample image will be loaded in every single item you have in the list. Just multiply it with the size of the image, that is how the app becomes a memory sucker. I made this mistake, and fortunately I used a really big image, so the exception was always raised.

Here is the example of pre set image resources

 <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 really big image. With default activity, my app only uses around 20mb of memory, enter image description here but after open the activity with only 3 items with that layout, it jumped into 90mb, scroll a little bit, OOM was raised. enter image description here So don’t forget to remove android:src after you finish preview, and you’ll probably save a good memory.

Hope it’s useful.

Use tools:src instead of android:src to get the preview image in Android Studio with zero impact on runtime http://tools.android.com/tips/layout-designtime-attributes

Resource Link:

Beware of Setting Image Resource for Preview in XML for Android

Upvotes: 4

Related Questions