2015-12-09 121 views
-3

我試圖調整列表視圖到alertDialog,該程序在dialog.setAdapter與nullPointerExeption崩潰。對話框列表適配器在nullPointerExeption崩潰

當用戶點擊listview中的項目時,alertDialog應該彈出。

ArrayAdapter<String> dialogAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userFunc); 
    lv.setAdapter(dialogAdapter); 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 


      if (position == 0) { 


       AlertDialog.Builder alertDialog = new AlertDialog.Builder(ManagerFunctions.this); 
       LayoutInflater inflater = getLayoutInflater(); 
       View convertView = (View) inflater.inflate(R.layout.user_op_list, null); 
       alertDialog.setView(convertView); 
       alertDialog.setTitle("Choose option"); 
       lv = (ListView) convertView.findViewById(R.id.lv); 

       alertDialog.show(); 

這行是一個我在收到錯誤:

 lv.setAdapter(dialogAdapter); 
+0

哪裏是你的錯誤日誌? – Amy

+0

@RaviRupareliya –

+0

初始化lv – Amy

回答

0

得到了它,我需要聲明我ArrayAdapter作爲最終,因爲它不能從內部類訪問。

這樣:

final String[] userFunc = new String[]{"חדש","מחק","ערוך"}; 
    final ArrayAdapter<String> dialogAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userFunc); 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 


      if (position == 0) { 


       AlertDialog.Builder alertDialog = new AlertDialog.Builder(ManagerFunctions.this); 
       LayoutInflater inflater = getLayoutInflater(); 
       View convertView = (View) inflater.inflate(R.layout.user_op_list, null); 
       alertDialog.setView(convertView); 
       alertDialog.setTitle("מה ברצונך לעשות"); 
       lv = (ListView) convertView.findViewById(R.id.lv); 
       lv.setAdapter(dialogAdapter); 

       alertDialog.show();