自定义alertdialog中listview中的空对象引用

时间:2015-04-29 17:47:17

标签: java android listview android-fragments android-listview

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

enter image description here

当我点击片段listview中的一个项目时出错。

enter image description here

如果我从listview的自定义布局中取消alertdialog,则显示alertdialog;但是,当我添加它时,会发生空对象引用错误。

我试图将我的所有代码放在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" />

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

0 个答案:

没有答案