Error on iniatialisation of Dialog box elements

Viewed 22

I'm working on an android project and I need to display data on ListView using Array data Now my dialog box is working, but when I define the list view, an error will display:

Process: com.example.uhf, PID: 31014

java.lang.NullPointerException: Attempt to invoke virtual method

'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

For now, I think that the problem is on finding LV id cause I use getView function so guys Can U help me to solve it?

My code:

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
ViewGroup viewGroup = getView().findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(v.getContext()).inflate(R.layout.custom_dialog_display_misssing_data, viewGroup, false);
builder.setView(dialogView);
AlertDialog alertDialog = builder.create();
ListView listViewDialog=(ListView)getView().findViewById(R.id.DialogLV);
String[] values = new String[] { "Android List View",
                                 "Adapter implementation",
                                 "Simple List View In Android",
                                 "Create List View Android",
                                 "Android Example",
                                 "List View Source Code",
                                 "List View Array Adapter",
                                 "Android Example List View"
                               };
ArrayAdapter<String> adapterLVDialog = new ArrayAdapter<String>(getContext(),
                                         android.R.layout.simple_list_item_1,
                                         android.R.id.text1, values);                                        
listViewDialog.setAdapter(adapterLVDialog);
alertDialog.show();

custom_dialog_display_missing_data.xml :

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="This List containts all missing tags elements"
    android:textAlignment="center"
/>
<ListView
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:id="@+id/DialogLV">
</ListView>
1 Answers

Process: com.example.uhf, PID: 31014 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

Don't

 ListView listViewDialog=(ListView)getView().findViewById(R.id.DialogLV);

Do

 ListView listViewDialog=(ListView)dialogView.findViewById(R.id.DialogLV);
Related