程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> layout-android對話框的根視圖

layout-android對話框的根視圖

編輯:編程綜合問答
android對話框的根視圖

android中的對話框,布局文件:

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


</ListView>

在Activity中給listView添加一個Adapter:

@Override
protected Dialog onCreateDialog(int id) {
    switch(id){
    case ADDPLAYERDIALOG:{
        Dialog d = new Dialog(this);            
        d.setContentView(R.layout.training_dialog);
        ListView lv = (ListView) d.getCurrentFocus().getRootView(); 
        ListAdapter adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, createNamesList());   
        lv.setAdapter(adapter);     

    return d;
    }
    }
    return super.onCreateDialog(id);
}

結果出現NullPointerException:

 ListView lv = (ListView) d.getCurrentFocus().getRootView();

沒有這個ListView小部件的ID,因為是XML布局文件,不能寫lv = d.findViewById(R.id.listview);

請高手指教。

最佳回答:


Dialog d = new Dialog(this);
ListView lv = (ListView) View.inflate(getBaseContext(),R.layout.training_dialog,null);
d.setContentView(lv);
ListAdapter adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, createNamesList());
lv.setAdapter(adapter);
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved