Я создал 2 фрагмента detailFrag и listFrag, но когда я пытаюсь скрыть / показать определенный фрагмент с помощью диспетчера фрагментов, findFragmentById(R.id.detailFrag)
и findFragmentById(R.id.listFrag)
возвращает нулевое значение. Я искал похожие проблемы, но ни одно из решений не помогло мне.
Здесь я вставляю код моего файла MainActivity, в котором генерируется ошибка:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvDescription=findViewById(R.id.tvDescription);
descriptions = getResources().getStringArray(R.array.descriptions);
if(findViewById(R.id.layout_portrait) !=null)
{
FragmentManager manager=this.getSupportFragmentManager();
manager.beginTransaction()
.hide(manager.findFragmentById(R.id.detailFrag))
.show(manager.findFragmentById(R.id.listFrag))
.commit();
}
if(findViewById(R.id.layout_land)!=null)
{
FragmentManager manager = this.getSupportFragmentManager();
manager.beginTransaction()
.show(manager.findFragmentById(R.id.detailFrag))
.show(manager.findFragmentById(R.id.listFrag))
.commit();
}
}
Здесь FragmentManager Я использовал класс Androidx.fragment.app , поскольку я не могу импортировать android .app.support.v4.Fragment Я пытался implementation 'com.android.support:support-v4:28.0.0'
в сборке Файл .gradle, но он показывает ошибку. Я должен был использовать библиотеку поддержки v4 здесь, но из-за этой проблемы я не могу использовать ее, не уверен, что это могло вызвать проблему.
XML Файлы:
MainActivity (портрет)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="horizontal"
android:id="@+id/layout_portrait"
tools:context=".MainActivity">
<fragment
android:id="@+id/listFrag"
android:name="com.example.fragmentspractice.ListFrag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_list" />
<fragment
android:id="@+id/detailFrag"
android:name="com.example.fragmentspractice.DetailFrag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="@layout/fragment_detail" />
</LinearLayout>
MainActivity (пейзаж):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="horizontal"
android:id="@+id/layout_land"
tools:context=".MainActivity">
<fragment
android:id="@+id/listFrag"
android:name="com.example.fragmentspractice.ListFrag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_list" />
<fragment
android:id="@+id/detailFrag"
android:name="com.example.fragmentspractice.DetailFrag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="@layout/fragment_detail" />
listFrag:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/listFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ListFrag">
<ListView
android:id="@+id/lvList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
detailFrag:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/detailFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:orientation="vertical"
tools:context=".DetailFrag">
<TextView
android:id="@+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</LinearLayout>