使用自定义列表视图打开对话框

时间:2013-01-08 08:20:59

标签: android android-listview android-dialog

我想在按钮点击时打开对话框。我的对话框有listview,它将显示每行带有文本的图像。但是,当我将适配器分配给我的listview时,它会抛出异常。

这是打开对话框的方法:

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();

    }

我的xml文件:

<?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文件:

<?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>

当我设置适配器时,它会抛出异常。那么问题是什么?请提出一些解决方案。

错误是:

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)

1 个答案:

答案 0 :(得分:1)

错误在这里

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

您正试图从主布局访问列表视图,而不是对话框的布局,您应该尝试类似的东西..

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