【问题标题】:Returning to the main fragment返回主片段
【发布时间】:2016-09-02 14:28:49
【问题描述】:

当我返回 nav_home 时,应用程序显示错误。可能会发生什么?

其他按钮都正常工作,只有nav_home不工作。

 public boolean onNavigationItemSelected(MenuItem item) {
            FragmentManager fm = getFragmentManager();
            int id = item.getItemId();

            if (id == R.id.nav_home) {
                fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
            } else if (id == R.id.nav_perfil) {
                fm.beginTransaction().replace(R.id.content_frame, new Perfil()).commit();
            }else if (id == R.id.nav_cadprof) {
                fm.beginTransaction().replace(R.id.content_frame, new CadProfFragment()).commit();
            } else if (id == R.id.nav_tabelapreco) {
                fm.beginTransaction().replace(R.id.content_frame, new TabelaPreco()).commit();

            } else if (id == R.id.nav_ajuda) {
                fm.beginTransaction().replace(R.id.content_frame, new Ajuda()).commit();

            } else if (id == R.id.nav_sobre) {
                fm.beginTransaction().replace(R.id.content_frame, new Sobre()).commit();

            } else if (id == R.id.nav_slogan) {

            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    }

日志

05-07 12:54:25.364 27082-27082/com.caseb.case_beauty_ E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.caseb.case_beauty_, PID: 27082
                                                                        android.view.InflateException: Binary XML file line #18: Error inflating class fragment

此 XML 在我的 ActivityMain 中的 onCreate 方法中调用,并且运行良好。应用程序调用此 Fragment 但更改 Fragment 并返回此 XML 应用程序显示错误。

XML 片段主要

<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.caseb.case_beauty_Fargments.MainFragment">

    <!-- TODO: Update blank fragment layout -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/map"
        android:orientation="vertical">

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="fill_parent"
            android:layout_height="300dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            tools:context="com.caseb.testemaps.MapsActivity"
            tools:layout="@layout/content_main" />

        <Button
            android:id="@+id/bt_estimavalor"
            android:layout_width="129dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@drawable/screen_border"
            android:backgroundTint="#66e72bac"
            android:text="Estimar Valor"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/bt_enviasolicita"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:background="@drawable/screen_border"
            android:backgroundTint="#66e72bac"
            android:text="Enviar Localização e Solicitar"
            android:textColor="#ffffff"
            android:textStyle="bold" />

        <ExpandableListView
            android:id="@+id/expandableListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</FrameLayout>

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    尝试清理您的项目,然后`invalidate Caches / Restart/.

    如果没有帮助,请查看此question

    【讨论】:

      【解决方案2】:

      根据他们的错误,xml中有错误。请如果您愿意添加代码以检查您的xml。问候

      【讨论】:

      • 好的,你可以使用这个 xml 来加载片段元素。请检查您使用此视图初始化的方法和对象。您可能会在 onResume () 和 onPause () 和实例方面出现问题。因为如果第一次充电好并再次显示该错误。这可以帮助activity 请让我知道
      【解决方案3】:

      从错误消息中可以得出结论,该片段的 xml 不正确。尝试在 AS 设计编辑器中打开 xml,看看有什么问题。

      【讨论】:

      • 描述中的XML,不要认为它有问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 2014-07-18
      相关资源
      最近更新 更多