java getActivity()。findviewbyd(R.id.myId)返回Null
所以我试图将一个自定义XML模板加载到片段中的RelativeLayout中,但XML视图返回null
我觉得我缺少了一个负责加载“描述”片段的类。“xml”。如果这是真的,我应该从哪里开始
这是我的主要片段,我想在其中加载我的布局 (函数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*/ }
});
}
}
片段列表项。xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".UI.ItemListFragment">
<RelativeLayout
安卓:id="@+id/rl_Description"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
/>
描述!。xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/rl_Description"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:background="@drawable/border">
<TextView
安卓:id="@+id/Label_Nom"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentStart="true"
安卓:layout_alignParentTop="true"
安卓:layout_marginStart="12dp"
安卓:layout_marginTop="12dp"
安卓:text="Nom de l'item:"
安卓:textColor="@color/black" />
<TextView
安卓:id="@+id/tv_NomItem"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/Label_Nom"
安卓:layout_alignParentStart="true"
安卓:layout_marginStart="12dp"
安卓:layout_marginTop="5dp"
tools:text="Burger" />
<TextView
安卓:id="@+id/Label_Description"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/tv_NomItem"
安卓:layout_alignParentStart="true"
安卓:layout_marginStart="12dp"
安卓:layout_marginTop="12dp"
安卓:text="Description de l'item:"
安卓:textColor="@color/black" />
<TextView
安卓:id="@+id/tv_DescriptionItem"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/Label_Description"
安卓:layout_alignParentStart="true"
安卓:layout_marginStart="12dp"
安卓:layout_marginTop="5dp"
tools:text="Allo le burger yer vrm bon: ceci est un exemple fyi" />
<TextView
安卓:id="@+id/Label_Prix"
安卓:layout_width="30dp"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/tv_DescriptionItem"
安卓:layout_alignParentStart="true"
安卓:layout_marginStart="12dp"
安卓:layout_marginTop="12dp"
安卓:text="Prix:"
安卓:textColor="@color/black" />
<TextView
安卓:id="@+id/tv_PrixItem"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/tv_DescriptionItem"
安卓:layout_marginStart="5dp"
安卓:layout_marginTop="12dp"
安卓:layout_toEndOf="@+id/Label_Prix"
tools:text="20$" />
<TextView
安卓:id="@+id/Label_Type"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/tv_DescriptionItem"
安卓:layout_marginStart="24dp"
安卓:layout_marginTop="12dp"
安卓:layout_toEndOf="@+id/tv_PrixItem"
安卓:text="Type d'item:"
安卓:textColor="@color/black" />
<TextView
安卓:id="@+id/tv_TypeItem"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/tv_DescriptionItem"
安卓:layout_marginStart="5dp"
安卓:layout_marginTop="12dp"
安卓:layout_toEndOf="@+id/Label_Type"
tools:text="Le type de l'item ici" />
<TextView
安卓:id="@+id/Label_Menu"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/tv_TypeItem"
安卓:layout_alignStart="@+id/Label_Nom"
安卓:layout_marginTop="48dp"
安卓:text="Menu que l'item fait partie:"
安卓:textColor="@color/black" />
<TextView
安卓:id="@+id/Label_MenuNom"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/Label_Menu"
安卓:layout_alignParentStart="true"
安卓:layout_marginStart="12dp"
安卓:layout_marginTop="12dp"
安卓:text="Nom du Menu:"
安卓:textColor="@color/black" />
<TextView
安卓:id="@+id/tv_NomMenu"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/Label_MenuNom"
安卓:layout_alignParentStart="true"
安卓:layout_marginStart="12dp"
安卓:layout_marginTop="5dp"
tools:text="Nom du menu ici" />
<TextView
安卓:id="@+id/Label_TypeMenu"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/tv_NomMenu"
安卓:layout_alignParentStart="true"
安卓:layout_marginStart="12dp"
安卓:layout_marginTop="12dp"
安卓:text="Type de menu:"
安卓:textColor="@color/black" />
<TextView
安卓:id="@+id/tv_TypeMenu"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/Label_TypeMenu"
安卓:layout_alignParentStart="true"
安卓:layout_marginStart="12dp"
安卓:layout_marginTop="12dp"
tools:text="Type du menu ici" />
共 (0) 个答案