Я пытаюсь получить экземпляр моего фрагмента для вызова некоторой функции внутри него.
Необычно я получаю нулевое значение из функции getFragmentManager (). FindFragmentById (id).
Однако в другом упражнении я могу получить экземпляр фрагмента с помощью той же функции getFragmentManager (). FindFragmentById (id).
Это моя не работающая активность java:
public class SetMapActivity extends AppCompatActivity {
public int imageId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_map);
clearSelected(null);
BoardFragment boardFragment = (BoardFragment)getFragmentManager().findFragmentById(R.id.sm_fragment); // It will be null
if(boardFragment.boardPositions.contains(R.drawable.sp)) // Null exception here
findViewById(R.id.sp).setEnabled(false);
if(boardFragment.boardPositions.contains(R.drawable.ep))
findViewById(R.id.ep).setEnabled(false);
}
}
А это не работающий xml действия:
<?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="com.example.user.myapplication.SetMapActivity"
tools:layout_editor_absoluteY="25dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<fragment
android:id="@+id/sm_fragment"
android:name="com.example.user.myapplication.BoardFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_board"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>