2013-01-08 82 views
1

我想打開按鈕單擊對話框。我的對話框有一個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

請發佈您的錯誤日誌。 – GrIsHu

+0

NullPointer異常 – zanky

回答

1

的錯誤是在這裏

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

你正試圖從主要佈局在訪問列表視圖,而不是對話的佈局,你應該嘗試的東西像..

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

哦,我得到了我的錯誤。 thanx你這麼多.. – zanky

相關問題