无法通过 id 或标签找到片段....但它在那里

Ste*_*ens 1 android android-fragments

我的主要活动有一个按钮和一个片段(注意按钮的 onclick 事件在此处分配:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="184dp"
        android:text="Button"
        android:onClick="onButtonClicked"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.steve.fragmentdemo.MyFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="159dp" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

该片段有一个文本视图、一个 id 和一个标签:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    tools:context=".MyFragment"
    android:id="@+id/myFragmentId"
    android:tag="myFragmentTag"
    >

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Soon to be overwritten" />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

该片段有一个公共函数来写入文本视图:

public class MyFragment extends Fragment {

    public MyFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my, container, false);
    }

    public void  setText(String theText){
        TextView text = getView().findViewById(R.id.myTextView);

        text.setText(theText);
    }
}
Run Code Online (Sandbox Code Playgroud)

主要活动可以到达片段但只能通过片段列表!

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onButtonClicked(View view)
    {
        FragmentManager supportFragmentManager = getSupportFragmentManager();

        MyFragment frameLayout;

        frameLayout = (MyFragment)supportFragmentManager.findFragmentById(R.id.myFragmentId);
        // Nope find by ID failed

        frameLayout = (MyFragment)supportFragmentManager.findFragmentByTag("myFragmentTag");
        // Nope find by tag failed

        List<Fragment> myList = supportFragmentManager.getFragments();

        frameLayout = (MyFragment)myList.get(0);

        //  And yet, the fragment is in the list of fragments
        if (frameLayout != null)
            frameLayout.setText("Hi There");   // this worked!
    }
}
Run Code Online (Sandbox Code Playgroud)

是什么赋予了?

仅供参考:我是一名经验丰富的开发人员,我正在学习 Android 是为了好玩……这有多恶心?

小智 5

此处的 id 应该是包含片段的布局,而不是一个实际片段。因此,真正的 id 应该是“片段”。