gpt4 book ai didi

java - getActivity().findViewById(R.id.myId) 返回 Null

转载 作者:行者123 更新时间:2023-11-29 18:41:02 28 4
gpt4 key购买 nike

因此,我尝试将自定义 XML 模板加载到我的 Fragment 中的 RelativeLayout 中,但 XML Views 返回 null。

我觉得这里缺少一个负责加载“description_fragment.xml”的类。如果那是真的,我应该从哪里开始?

这是我要加载布局的主要 fragment (创建此 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 列表项目.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"
/>

描述_ 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" />

最佳答案

您正在尝试通过 Activity 查找 View ,但您的 Activity 不知道 fragment xml 及其 View 。

您应该执行 view.findViewById(id),其中 view 是在 onCreateView 中创建的 fragment View 。

例如,您可以在 onCreateView 中定义所有 View 并将其保存到变量中,然后您可以在 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*/ }
});
}

关于java - getActivity().findViewById(R.id.myId) 返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53074286/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com