因此,我试图将自定义XML模板加载到Fragment的RelativeLayout中,但是XML视图返回null。
我觉得在这里我缺少一个负责加载'description_fragment.xml'的类。如果是这样,我应该从哪里开始?
这是我的主要片段,我要将布局加载到其中 (在创建此Fragment之后,将调用loadDescription()函数):
public class ItemListFragment extends Fragment{
RelativeLayout relativeDescription;
item mItemDesc = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
//The whole fragment
RelativeLayout liste_item = (RelativeLayout)inflater.inflate(R.layout.fragment_liste_item,container,false);
//The item description fragment
relativeDescription = (RelativeLayout) inflater.inflate(R.layout.description_item,container, false);
return liste_item;
}
public void loadDescription(int itemSelectonne) {
//Http Request
ItemClient client = ServiceGenerator.retrofit.create(ItemClient.class);
Call<item> list = client.getDescriptionItem(Authentification.getXauth(), itemSelectonne);
//Async Task
list.enqueue(new Callback<item>() {
@Override
public void onResponse(Call<item> call, Response<item> response) {
if(response.isSuccessful())
{
//Set response from request into (item) variable
mItemDesc = response.body();
//new var (data) used in case of Desync
item data = mItemDesc;
//Obtaining the RelativeView from the fragment_list_item.xml file
relativeDescription = getActivity().findViewById(R.id.rl_Description);
//Obtainaing all the Views inside description_item.xml
TextView NomItem = getActivity().findViewById(R.id.tv_NomItem); //This returns null
NomItem.setText(data.getNom());
TextView DescriptionItem = getActivity().findViewById(R.id.tv_DescriptionItem);
DescriptionItem.setText(data.getDescription());
TextView PrixItem = getActivity().findViewById(R.id.tv_PrixItem);
PrixItem.setText(data.getPrix());
TextView TypeItem = getActivity().findViewById(R.id.tv_TypeItem);
TypeItem.setText(data.getTypeItem());
TextView NomMenu = getActivity().findViewById(R.id.tv_NomMenu);
NomMenu.setText(data.getMenu().getNom());
TextView TypeMenu = getActivity().findViewById(R.id.tv_TypeMenu);
TypeMenu.setText(data.getMenu().getType());
}
else { /*HTTP Errors*/ }
}
}
@Override
public void onFailure(Call<item> call, Throwable t) { /*Message*/ }
});
}
}
fragment_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".UI.ItemListFragment">
<RelativeLayout
android:id="@+id/rl_Description"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
description_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_Description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border">
<TextView
android:id="@+id/Label_Nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:text="Nom de l'item:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_NomItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Label_Nom"
android:layout_alignParentStart="true"
android:layout_marginStart="12dp"
android:layout_marginTop="5dp"
tools:text="Burger" />
<TextView
android:id="@+id/Label_Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_NomItem"
android:layout_alignParentStart="true"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:text="Description de l'item:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_DescriptionItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Label_Description"
android:layout_alignParentStart="true"
android:layout_marginStart="12dp"
android:layout_marginTop="5dp"
tools:text="Allo le burger yer vrm bon: ceci est un exemple fyi" />
<TextView
android:id="@+id/Label_Prix"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_DescriptionItem"
android:layout_alignParentStart="true"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:text="Prix:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_PrixItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_DescriptionItem"
android:layout_marginStart="5dp"
android:layout_marginTop="12dp"
android:layout_toEndOf="@+id/Label_Prix"
tools:text="20$" />
<TextView
android:id="@+id/Label_Type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_DescriptionItem"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:layout_toEndOf="@+id/tv_PrixItem"
android:text="Type d'item:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_TypeItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_DescriptionItem"
android:layout_marginStart="5dp"
android:layout_marginTop="12dp"
android:layout_toEndOf="@+id/Label_Type"
tools:text="Le type de l'item ici" />
<TextView
android:id="@+id/Label_Menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_TypeItem"
android:layout_alignStart="@+id/Label_Nom"
android:layout_marginTop="48dp"
android:text="Menu que l'item fait partie:"
android:textColor="@color/black" />
<TextView
android:id="@+id/Label_MenuNom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Label_Menu"
android:layout_alignParentStart="true"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:text="Nom du Menu:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_NomMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Label_MenuNom"
android:layout_alignParentStart="true"
android:layout_marginStart="12dp"
android:layout_marginTop="5dp"
tools:text="Nom du menu ici" />
<TextView
android:id="@+id/Label_TypeMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_NomMenu"
android:layout_alignParentStart="true"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:text="Type de menu:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_TypeMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Label_TypeMenu"
android:layout_alignParentStart="true"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
tools:text="Type du menu ici" />
答案 0 :(得分:1)
您正在尝试通过活动查找视图,但是您的活动不知道片段xml及其视图。
您应该执行view.findViewById(id)
,其中view
是在onCreateView
中创建的片段视图。
例如,您可以在onCreateView
中定义所有视图并将其保存到变量中,然后可以在loadDescription
方法中对其进行交互。
public class ItemListFragment extends Fragment{
RelativeLayout relativeDescription;
TextView NomItem;
TextView DescriptionItem;
// and so on
item mItemDesc = null;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//The whole fragment
View view = (RelativeLayout)inflater.inflate(R.layout.fragment_liste_item,container,false);
//The item description fragment
relativeDescription = (RelativeLayout) inflater.inflate(R.layout.description_item,container, false);
NomItem = view.findViewById(R.id.tv_NomItem);
DescriptionItem = view.findViewById(R.id.tv_DescriptionItem);
// and so on
return liste_item;
}
public void loadDescription(int itemSelectonne) {
//Http Request
ItemClient client = ServiceGenerator.retrofit.create(ItemClient.class);
Call<item> list = client.getDescriptionItem(Authentification.getXauth(), itemSelectonne);
//Async Task
list.enqueue(new Callback<item>() {
@Override
public void onResponse(Call<item> call, Response<item> response) {
if(response.isSuccessful())
{
//Set response from request into (item) variable
mItemDesc = response.body();
//new var (data) used in case of Desync
item data = mItemDesc;
//This returns null
NomItem.setText(data.getNom());
DescriptionItem.setText(data.getDescription());
// and so on
}
else { /*HTTP Errors*/ }
}
}
@Override
public void onFailure(Call<item> call, Throwable t) { /*Message*/ }
});
}