Reputation: 1231
I have a custom layout for a alertdialog
. Inside I have a listview
. To open this alertdialog
I have another list inside a fragment.
I have a error when I tap one item from the fragment listview
.
If I take off the listview
from the custom layout of the alertdialog
, the alertdialog
shows up; however, when I add it, the null object reference error occurs.
I tried to put all the my code inner onCreateView()
but it is still failing.
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" />
And 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" />
Please I need some support with this error.
Upvotes: 2
Views: 1255
Reputation: 128428
Problem:
You are getting NullPointerException
just because you are trying to find ListView in an activity, not in a dialog view actually.
// 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);
Solution:
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);
Upvotes: 3
Reputation: 157447
your problem lies here
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);
If I understand correcty the buildingLinkListView
is part of the the dialog not of the Activity. Change it like:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = null;
builder.setView(view = inflater.inflate(R.layout.custom_dialog_building,null));
ListView buildingLinkListView = (ListView) view.findViewById(R.id.buildingLinkListView);
I am assigning the inflated view in the setView call
setView(view = inflater.
Upvotes: 1