我正在嘗試使用導航圖在兩個片段之間導航。由于某種原因,當我使用圖形的生成操作調用導航方法時,目標片段正在“創建”但沒有出現,而起始片段就像已被停用一樣,但不會消失。我想我已經遵循了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
提交
取消