Java getActivity().findViewById(R.id.myId)返回Null
因此,我试图将一个自定义XML模板加载到片段中的RelativeLayout中,但XML视图返回null 我觉得这里缺少了一个负责加载“description_fragment.xml”的类。如果这是真的,我应该从哪里开始 这是我的主要片段,我想在其中加载布局 (函数loadDescription()在创建此片段后调用):Java getActivity().findViewById(R.id.myId)返回Null,java,android,xml,android-fragments,Java,Android,Xml,Android Fragments,因此,我试图将一个自定义XML模板加载到片段中的RelativeLayout中,但XML视图返回null 我觉得这里缺少了一个负责加载“description_fragment.xml”的类。如果这是真的,我应该从哪里开始 这是我的主要片段,我想在其中加载布局 (函数loadDescription()在创建此片段后调用): 公共类ItemListFragment扩展了片段{ 相对描述; item mItemDesc=null; @凌驾 创建时的公共void(Bundle savedInstanc
公共类ItemListFragment扩展了片段{
相对描述;
item mItemDesc=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
创建视图时的公共视图(@NonNull layoutiner flater flatter,ViewGroup container,
Bundle savedInstanceState){
//整个片段
RelativeLayout列表项=(RelativeLayout)充气器。充气(R.layout.fragment列表项,容器,false);
//项目描述片段
relativeDescription=(RelativeLayout)充气机充气(R.layout.description\u项目,容器,假);
返回列表项;
}
公共无效加载说明(int itemSelectonne){
//Http请求
ItemClient client=ServiceGenerator.Reformation.create(ItemClient.class);
调用列表=client.getDescriptionItem(authentication.getXauth(),itemSelectonne);
//异步任务
list.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful())
{
//将请求的响应设置为(项)变量
mItemDesc=response.body();
//失同步情况下使用的新var(数据)
项目数据=mItemDesc;
//从fragment_list_item.xml文件获取RelativeView
relativeDescription=getActivity().findViewById(R.id.rl_Description);
//获取description_item.xml中的所有视图
TextView NomItem=getActivity().findviewbyd(R.id.tv_NomItem);//返回null
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错误*/}
}
}
@凌驾
public void onFailure(调用,可丢弃的t){/*消息*/}
});
}
}
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\u 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" />
您试图通过活动查找视图,但您的活动不知道片段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*/ }
});
}
公共类ItemListFragment扩展了片段{
相对描述;
文本视图名称项;
文本视图描述;
//等等
item mItemDesc=null;
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、ViewGroup容器、Bundle savedInstanceState){
//整个片段
视图=(RelativeLayout)充气器。充气(R.layout.fragment\u liste\u item,container,false);
//项目描述片段
relativeDescription=(RelativeLayout)充气机充气(R.layout.description\u项目,容器,假);
NomItem=view.findviewbyd(R.id.tv_NomItem);
DescriptionItem=view.findViewById(R.id.tv\u DescriptionItem);
//等等
返回列表项;
}
公共无效加载说明(int itemSelectonne){
//Http请求
ItemClient client=ServiceGenerator.Reformation.create(ItemClient.class);
调用列表=client.getDescriptionItem(authentication.getXauth(),itemSelectonne);
//异步任务
list.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful())
{
//将请求的响应设置为(项)变量
mItemDesc=response.body();
//失同步情况下使用的新var(数据)
项目数据=mItemDesc;
//这将返回null
setText(data.getNom());
DescriptionItem.setText(data.getDescription());
//等等
}
else{/*HTTP错误*/}
}
}
@凌驾
public void onFailure(调用,可丢弃的t){/*消息*/}
});
}
您可以添加错误日志吗?您在onCreate()
和loadDescription()中设置了relativeDescription
,但从未使用过它?也许你应该尝试liste\u item.findViewById()
或relativeDescription.findViewById()
或在onCreateView()
返回后的任何时间调用getView()
。@TheLoverter Fair point,但是,您可能只希望在每个onViewCreated
回调中查找视图id一次,并保留引用(特别是在它是一个复杂的视图层次结构时),而不是每次需要时getView().findBy..
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*/ }
});
}