我正在尝试使用导航图在两个片段之间导航。由于某种原因,当我使用图形的生成操作调用导航方法时,目标片段正在“创建”但没有出现,而起始片段就像已被停用一样,但不会消失。我想我已经遵循了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" />...
2 回答
陪伴而非守候
TA贡献1757条经验 获得超8个赞
这:
// 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
TA贡献2036条经验 获得超8个赞
如果您使用数据绑定,请更改充气机线这些线
从
mDataBinding=DataBindingUtil.inflate(getLayoutInflater(),R.layout.fragment_date_sheet, container, false);
到
mDataBinding= DataBindingUtil.inflate(inflater,R.layout.fragment_date_sheet, container, false);
添加回答
举报
0/150
提交
取消