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

时间:2018-04-16 02:04:59

标签: 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>

该片段有一个textview,一个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>

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

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);
    }
}

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

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!
    }
}

是什么给出了?

仅供参考:我是一位经验丰富的开发人员,我正在学习Android以获得乐趣......这有多恶心?

1 个答案:

答案 0 :(得分:2)

这里的id应该是包含片段而不是一个实际片段的布局。因此,真实的id应该是&#34;片段&#34;。