我想在单击按钮时打开对话框。我的对话框具有列表视图,它将显示一个图像,每行带有文本。但是当我将适配器分配给我的列表视图时,它会引发异常。
这是我打开对话框的方法:
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(AndroidBuildingMusicPlayerActivity.java:1346)
01-08 13:56:53.835: E/AndroidRuntime(734): at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity$15.onClick(AndroidBuildingMusicPlayerActivity.java:481)
01-08 13:56:53.835:
E/AndroidRuntime(734): at android.view.View.performClick(View.java:4084)