![](/img/trans.png)
[英]Text not appearing in Fragment view in Android Navigation drawer app
[英]Android Destination Fragment not Appearing on Navigation
我正在嘗試使用導航圖在兩個片段之間導航。 出於某種原因,當我使用圖形生成的操作調用導航方法時,目標片段正在“創建”但沒有出現,而起始片段就好像它已被停用但沒有消失一樣。 我想我已經按照谷歌文檔頁面上列出的所有步驟進行了操作,但我可能遺漏了一些簡單的東西!
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_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" />
...
這是我的導航圖:
<?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_width="match_parent"
android:layout_height="match_parent"
tools:context=".frag.MealCostFrag">
<EditText
android:id="@+id/editText01"
android:layout_width="0dp"
android:layout_height="55dp"
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_width="87dp"
android:layout_height="46dp"
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()”方法內部的日志,但它只是沒有顯示。
我知道我已經為大家提供了大量代碼供大家篩選,因此我將特別感謝願意花一些時間幫助我的任何人。 我會根據要求提供任何額外的必要信息。 提前謝謝大家,希望這是我犯的一個可笑的微不足道的錯誤!
正如我所懷疑的那樣,我犯了一個非常簡單的錯誤。 我在目標片段上膨脹的視圖是我的開始片段的視圖。 只是一個復制和粘貼錯誤。
這個:
// 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);
這里學到的教訓是在調試時仔細檢查過程的每一步! 錯誤通常隱藏在顯而易見的地方。
如果您正在使用數據綁定,請更改充氣線這些線
從
mDataBinding=DataBindingUtil.inflate(getLayoutInflater(),R.layout.fragment_date_sheet, container, false);
到
mDataBinding= DataBindingUtil.inflate(inflater,R.layout.fragment_date_sheet, container, false);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.