gpt4 book ai didi

java - 自定义警报对话框中 ListView 中的空对象引用

转载 作者:搜寻专家 更新时间:2023-11-01 07:54:34 26 4
gpt4 key购买 nike

我有一个 alertdialog 的自定义布局。我在里面有一个 listview。要打开此 alertdialog,我在一个 fragment 中有另一个列表。

enter image description here

当我从 fragment listview 中点击一个项目时出现错误。

enter image description here

如果我从 alertdialog 的自定义布局中删除 listviewalertdialog 就会出现;但是,当我添加它时,出现空对象引用错误。

我试图将我所有的代码都放在 onCreateView() 中,但它仍然失败。

BuildingFragment.java

public class BuildingFragment extends Fragment {


public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

final Context context = getActivity().getBaseContext();
final ListView listview2 = (ListView) getView().findViewById(R.id.listView3);

listview2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(inflater.inflate(R.layout.custom_dialog_building,null));


ListView buildingLinkListView = (ListView) getActivity().findViewById(R.id.buildingLinkListView);
ArrayList<BuildingLinkItem> buildingLinkList = new ArrayList<>();
//....
// Getting items for buildingLinkList
//....

BuildingLinkAdapter buildingLinkAdapter = new BuildingLinkAdapter(getActivity(),buildingLinkList);
buildingLinkListView.setAdapter(buildingLinkAdapter);

AlertDialog dialog = builder.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show();

}
});

custom_dialog_building.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customdialog"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_bg_navigation"/>

<ListView
android:id="@+id/buildingLinkListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

list_item_building_link.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/list_item_building_icon"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Large Text"
android:id="@+id/list_item_building_text"
android:gravity="center_vertical"
android:layout_marginLeft="16dp"
android:textSize="20sp"
android:layout_toRightOf="@+id/list_item_building_icon" />

这个错误我需要一些支持。

最佳答案

问题:您得到 NullPointerException 只是因为您试图在 Activity 中而不是在对话框 View 中查找 ListView。

// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(inflater.inflate(R.layout.custom_dialog_building,null)); ListView buildingLinkListView = (ListView) getActivity().findViewById(R.id.buildingLinkListView);

解决方案:

LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.custom_dialog_building,null)

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);

ListView buildingLinkListView = (ListView) view.findViewById(R.id.buildingLinkListView);

关于java - 自定义警报对话框中 ListView 中的空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29950894/

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