Android 目的地片段未出现在导航中

Posted

技术标签:

【中文标题】Android 目的地片段未出现在导航中【英文标题】:Android Destination Fragment not Appearing on Navigation 【发布时间】:2020-01-16 07:02:33 【问题描述】:

我正在尝试使用导航图在两个片段之间导航。出于某种原因,当我使用图形的生成操作调用导航方法时,目标片段正在“创建”但没有出现,而起始片段只是坐在那里,好像它已被停用但不会消失。我想我已经完成了Google's documentation page for this 中列出的所有步骤,但我可能遗漏了一些简单的东西!

这是我的依赖项:

dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    // ViewModel and LiveData
    def lifecycle_version = "2.1.0"
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"

    def nav_version = "2.1.0"
    implementation "androidx.navigation:navigation-fragment:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

主要活动

在我的主要活动类中,我只是在OnCreate() 中定义了一个ModelView

public class MainActivity extends AppCompatActivity 
    MealDataModel mealDataModel;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the model on create of the main activity
        mealDataModel = ViewModelProviders.of(this).get(MealDataModel.class);
    

在我的主要活动布局中,我只是使用 NavHostFragment:

...
<fragment
        android:id="@+id/fragment2"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_
        android:layout_
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />
...

导航图

这是我的导航图:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
    app:startDestination="@id/mealCostFrag">

    <fragment
        android:id="@+id/mealCostFrag"
        android:name="com.finley.program2.frag.MealCostFrag"
        android:label="fragment_meal_cost"
        tools:layout="@layout/fragment_meal_cost" >
        <action
            android:id="@+id/action_mealCostFrag_to_tipPercentFrag"
            app:destination="@id/tipPercentFrag" />
    </fragment>
    <fragment
        android:id="@+id/tipPercentFrag"
        android:name="com.finley.program2.frag.TipPercentFrag"
        android:label="fragment_tip_percent"
        tools:layout="@layout/fragment_tip_percent" />
</navigation>

开始片段

我的导航图的起始目的地是这个片段...:

public class MealCostFrag extends Fragment 
    private MealDataModel mealDataModel;
    private EditText etCost;
    private Button btnNext;
    private double cost;
    public MealCostFrag() 
        // Required empty public constructor
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        // Inflate the layout for this fragment
        View myView = inflater.inflate(R.layout.fragment_meal_cost, container, false);
        mealDataModel = ViewModelProviders.of(getActivity()).get(MealDataModel.class);

        // Set listeners and such...
        etCost = myView.findViewById(R.id.editText01);
        btnNext= myView.findViewById(R.id.button01);
        btnNext.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                try 
                    cost = Double.parseDouble(etCost.getText().toString());
                    if (cost <= 0) throw new Exception();
                    mealDataModel.setMealCost(cost);
                    Log.i("COST FRAG","Navigating to tip pct with cost = " + mealDataModel.getMealCost());
                    Navigation.findNavController(v).navigate(R.id.action_mealCostFrag_to_tipPercentFrag);
                 catch (Exception e) 
                    Log.e("COST FRAG", "Invalid cost entered, cost = " + cost);
                    Toast.makeText(getContext(),
                            "Please enter a valid meal cost.", Toast.LENGTH_SHORT).show();
                
            
        );
        return myView;

    


使用这种布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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_
    android:layout_
    tools:context=".frag.MealCostFrag">

    <EditText
        android:id="@+id/editText01"
        android:layout_
        android:layout_
        android:layout_marginStart="64dp"
        android:layout_marginLeft="64dp"
        android:layout_marginTop="48dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:ems="10"
        android:gravity="center_horizontal|center_vertical"
        android:hint="Cost"
        android:inputType="number|numberDecimal"
        android:textSize="24sp"
        app:layout_constraintEnd_toStartOf="@+id/button01"
        app:layout_constraintHorizontal_bias="1.0"/>

    <Button
        android:id="@+id/button01"
        android:layout_
        android:layout_
        android:layout_marginEnd="64dp"
        android:layout_marginRight="64dp"
        android:text="Next"
        app:layout_constraintBaseline_toBaselineOf="@+id/editText01"
        app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

目标片段:

这是我要导航到的片段,布局为空:

public class TipPercentFrag extends Fragment 
    private MealDataModel mealDataModel;
    public TipPercentFrag() 
        // Required empty public constructor
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        // Inflate the layout for this fragment
        View myView = inflater.inflate(R.layout.fragment_meal_cost, container, false);
        mealDataModel = ViewModelProviders.of(getActivity()).get(MealDataModel.class);

        // Set listeners and such...
        Log.i("TIP PCT FRAG", "CreateView of tip pct frag called");

        return myView;

    

我觉得奇怪的是目标片段的“onCreateView()”方法内部的日志正在输出,但它只是没有显示。

我知道我提供了一大堆代码供大家筛选,所以我会非常感谢愿意花时间帮助我的人。我会根据要求提供任何额外的必要信息。提前谢谢大家,希望这是我犯的一个可笑的微不足道的错误!

【问题讨论】:

【参考方案1】:

正如我所怀疑的,我犯了一个非常简单的错误。我在目标片段上膨胀的视图是我的起始片段的视图。只是复制和粘贴错误。

这个:

// Inflate the layout for this fragment
        View myView = inflater.inflate(R.layout.fragment_meal_cost, container, false);
        mealDataModel = ViewModelProviders.of(getActivity()).get(MealDataModel.class);

只需要这样:

// Inflate the layout for this fragment
        View myView = inflater.inflate(R.layout.fragment_tip_percent, container, false);
        mealDataModel = ViewModelProviders.of(getActivity()).get(MealDataModel.class);

这里学到的教训是在调试时仔细检查过程的每一步!错误通常隐藏在显而易见的地方。

【讨论】:

【参考方案2】:

如果您使用 DataBinding,请更改这些行的充气线

来自

mDataBinding=DataBindingUtil.inflate(getLayoutInflater(),R.layout.fragment_date_sheet, container, false);

mDataBinding= DataBindingUtil.inflate(inflater,R.layout.fragment_date_sheet, container, false);

【讨论】:

以上是关于Android 目的地片段未出现在导航中的主要内容,如果未能解决你的问题,请参考以下文章

如何从一个片段导航到另一个片段?

Android Jetpack导航,另一个主机片段内的主机片段

Android导航组件后退按钮不起作用

从另一个图表导航到片段,而不是起始目的地

Android Jetpack Navigation:如何在 OnNavigatedListener 中获取目的地的片段实例?

未创建 Android listview 内容视图