Android 目标片段未出现在导航上

我正在尝试使用导航图在两个片段之间导航。由于某种原因,当我使用图形的生成操作调用导航方法时,目标片段正在“创建”但没有出现,而起始片段就像已被停用一样,但不会消失。我想我已经遵循了Google 文档页面上列出的所有步骤,但我可能遗漏了一些简单的东西!

这是我的依赖项:

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'

}

主要活动

在我的主要活动类中,我只是定义了ModelViewin OnCreate():


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_width="match_parent"

        android:layout_height="match_parent"

        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" />

...


RISEBY
浏览 83回答 2
2回答

陪伴而非守候

这:// 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);这里学到的教训是在调试时仔细检查过程的每一步!错误通常隐藏在显而易见的地方。

慕桂英3389331

如果您使用数据绑定,请更改充气机线这些线从mDataBinding=DataBindingUtil.inflate(getLayoutInflater(),R.layout.fragment_date_sheet,&nbsp;container,&nbsp;false);到mDataBinding=&nbsp;DataBindingUtil.inflate(inflater,R.layout.fragment_date_sheet,&nbsp;container,&nbsp;false);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java