我正在为Android创建一个简单的应用。
我有一个主要活动和两个片段。
Main布局包含一个片段容器:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#6940">
<Button
android:id="@+id/prevBtn"
android:layout_width="160dp"
android:layout_height="40dp"
android:layout_marginBottom="16dp"
android:layout_marginStart="20dp"
android:clickable="false"
android:onClick="prevWord"
android:text="Previous"
android:background="#ff4444"
android:textColor="@android:color/white"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/nextBtn"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/nextBtn"
android:layout_width="160dp"
android:layout_height="40dp"
android:layout_marginBottom="16dp"
android:layout_marginRight="36dp"
android:background="#ff4444"
android:clickable="false"
android:onClick="nextWord"
android:text="Next"
android:textColor="@android:color/white"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<ImageView
android:id="@+id/wordImage"
android:layout_width="275dp"
android:layout_height="219dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
app:layout_constraintBottom_toTopOf="@+id/radioButton1"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="NotSibling" />
<Button
android:id="@+id/audioBtn"
android:layout_width="136dp"
android:layout_height="40dp"
android:layout_marginStart="32dp"
android:layout_marginTop="79dp"
android:background="@android:color/holo_green_light"
android:onClick="playAudio"
android:text="Audio"
android:textSize="24sp"
app:layout_constraintLeft_toLeftOf="@+id/radioWords"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/wordImage" />
<RadioGroup
android:id="@+id/radioWords"
android:layout_width="wrap_content"
android:layout_height="174dp"
android:layout_marginTop="20dp"
app:layout_constraintHorizontal_bias="0.778"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/wordImage">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="8dp"
android:checked="false"
android:text="@string/word1"
android:textSize="34sp"
app:layout_constraintBottom_toTopOf="@+id/radioButton2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/wordImage" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/word2"
android:textSize="34sp"
app:layout_constraintBottom_toTopOf="@+id/radioButton3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/word3"
android:textSize="34sp"
app:layout_constraintBottom_toTopOf="@+id/audioBtn"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</RadioGroup>
<RelativeLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="238dp"
android:layout_marginTop="10dp"
app:layout_constraintTop_toTopOf="parent">
</RelativeLayout>
这两个片段的布局和类都已创建。其中之一是:
public class resultsFrag extends android.app.Fragment {
public static TextView attempts, correct;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.resultsfragment, container, false);
attempts = view.findViewById(R.id.attemptsTxt);
correct = view.findViewById(R.id.correctTxt);
//((MainActivity)getActivity())。setViewPager(0);
return view;
}
}
现在,从我的MainActivity,我想将片段容器设置为fragment1,然后设置为fragment2,并在字段'尝试'和'正确'中设置相应的值。
FragmentManager fragmentMgr = getFragmentManager();
android.app.FragmentTransaction fragTransaction = fragmentMgr.beginTransaction();
niceWorkFrag nwork = new niceWorkFrag();
fragTransaction.add(R.id.fragmentContainer, nwork);
fragTransaction.commit();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
fragmentMgr = getFragmentManager();
fragTransaction = fragmentMgr.beginTransaction();
resultsFrag res = new resultsFrag();
fragTransaction.replace(R.id.fragmentContainer, res);
fragTransaction.commit();
但我无法访问字段'尝试'和'正确'。变量总是为空。
TextView att = findViewById(R.id.attemptsTxt);
att.setText(attemptsTotal);
TextView corr = findViewById(R.id.correctTxt);
corr.setText(correct);
答案 0 :(得分:0)
最后,我不得不在Bundle中传递变量。