Zankhna
Zankhna

Reputation: 4563

Open Dialog With Custom Listview

I want to open dialog on button click. My dialog is having listview which will display one image with text per row. But when i assign adapter to my listview it throws exception.

Here is my method to open dialog :

public void openShareDialog()

    {
        Dialog d = new Dialog(this);
        d.setTitle("Select Color Mode");
        d.setContentView(R.layout.share_list);

        String s[] = { "Facebook", "Twitter" };
        int image[] = { R.drawable.facebook, R.drawable.twitter };

        ArrayList<HashMap<String, String>> objArayList = new ArrayList<HashMap<String, String>>();

        for (int i = 0; i < 2; i++) {
            HashMap<String, String> listData = new HashMap<String, String>();
            listData.put("text", s[i]);
            listData.put("image", Integer.toString(image[i]));

            objArayList.add(listData);

        }

        String[] from = { "image", "text" };
        int[] to = { R.id.list_image, R.id.text };

        SimpleAdapter listAdapter = new SimpleAdapter(context, objArayList,
                R.layout.share_list_item, from, to);
        ListView lst1 = (ListView) findViewById(android.R.id.list);

        lst1.setAdapter(listAdapter);
        lst1.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

            }
        });

        d.show();

    }

my xml file :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="#000000"
        android:dividerHeight="2dp" />

</RelativeLayout>

xml file for list row :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/listmusicicon" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/list_image"
        android:maxLines="1" />

</RelativeLayout>

When i set adapter it throws exception. So what is the problem ? Please suggest some solution.

The error is:

01-08 13:56:53.835: 
E/AndroidRuntime(734): 
FATAL EXCEPTION: main 01-08 13:56:53.835: 
E/AndroidRuntime(734): java.lang.NullPointerException 01-08 13:56:53.835: 
E/AndroidRuntime(734): at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity.openShareDialog(A‌​ndroidBuildingMusicPlayerActivity.java:1346) 
01-08 13:56:53.835: E/AndroidRuntime(734): at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity$15.onClick(Androi‌​dBuildingMusicPlayerActivity.java:481) 
01-08 13:56:53.835: 
E/AndroidRuntime(734): at android.view.View.performClick(View.java:4084)

Upvotes: 1

Views: 3854

Answers (1)

Nermeen
Nermeen

Reputation: 15973

The error is here

ListView lst1 = (ListView) findViewById(android.R.id.list);

You are trying to access the list view from the main layout, not the dialog's layout, you should try something like..

ListView lst1 = (ListView) d.findViewById(android.R.id.list);

Upvotes: 1

Related Questions