我正在尝试使用导航图在两个片段之间导航。出于某种原因,当我使用图的生成的动作调用Navigation方法时,目标片段正在“创建”但没有出现,而起始片段只是就好像它已被停用但没有消失一样。我认为我已经按照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'
}
在我的主要活动课程中,我只是在ModelView
中定义一个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" />
...
这是我的导航图:
<?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()”方法中的日志,但它并未显示。
我知道我已经为大家提供了大量的代码来进行筛选,因此我对任何愿意花时间帮助我的人都非常感激。我将根据要求提供任何其他必要的信息。预先谢谢大家,希望这是我犯的一个小错误!
答案 0 :(得分:0)
就像我怀疑的那样,我犯了一个非常简单的错误。我在目标片段上夸大的视图是我的起始片段的视图。只是复制和粘贴错误。
此:
// 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);
在此汲取的教训是在调试时仔细检查过程的每个步骤!错误的确通常隐藏在视线范围内。
答案 1 :(得分:0)
如果您使用数据绑定,请更改充气线这些线
来自
mDataBinding=DataBindingUtil.inflate(getLayoutInflater(),R.layout.fragment_date_sheet, container, false);
到
mDataBinding= DataBindingUtil.inflate(inflater,R.layout.fragment_date_sheet, container, false);