我是 android 开发新手,可能需要您的帮助。 我正在使用带有 fragment 的 FrameLayout 进行导航。 XML 文件是正确的(或者更确切地说,它们在 IDE 中显示正确)。 但是加载 fragment 不起作用。并且没有任何错误可以帮助我。
唯一的错误是每次加载 fragment 时,调试器都会说:
W/PathParser: Points are too far apart 4.000000596046461
XML 是 Android Studio 提供的非常基本的 XML(带有 HelloWorld-Textbox 的模板)。我正在使用以下代码加载 fragment :
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new Dashboard());
fragmentTransaction.commit();
R.id.fragment_container
是正确的(首先我没有那么多我可能会混淆的东西,其次我检查了两次)。
非常感谢您的宝贵时间,希望对我有所帮助。如果您需要任何进一步的信息,请向他们索取。
添加: Fragment(一生效就改):
<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>
主要内容:
<?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>
最佳答案
这在 Android 中确实是图像的常见问题。
有时候,当我们写一个布局文件时,我们想要预览结果,如果布局有一个ImageView,那么我们也想通过在xml中提供图像资源来查看图像。请注意,当您在 ImageView 中设置图像资源仅用于预览目的时,请不要忘记将其删除。如果我们忘记删除它,那么在运行时图像将被加载并吸取应用程序内存。
如果图像只加载一次似乎没问题,但如果示例图像在项目布局上,并且我们在回收器 View ( ListView )中使用布局怎么办?示例图像将加载到列表中的每个项目中。只需将它乘以图像的大小,这就是应用程序如何成为内存吸盘的原因。 我犯了这个错误,幸运的是我使用了一个非常大的图像,所以总是引发异常。
这里是预设图片资源的例子
<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
是一张非常大的图片。对于默认 Activity ,我的应用仅使用大约 20mb
内存,
但在打开只有 3 个项目的 Activity 后,它跳到 90mb
,滚动一点,OOM 提高了。
所以不要忘记在完成预览后删除 android:src
,您可能会留下美好的记忆。
希望有用。
使用 tools:src
而不是 android:src
在 Android Studio 中获取预览图像,对运行时的影响为零 http://tools.android.com/tips/layout-designtime-attributes
资源链接:
Beware of Setting Image Resource for Preview in XML for Android
关于java - Android FragmentLayout 未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38508146/