android - setAdapter() causes NullPointerException in (Sherlock)DialogFragment -


i'm trying set arrayadapter listview inside oncreatedialog() method of (sherlock)dialogfragment. i'm getting nullpointerexception think path layout , string-array correct, because when set adapter directly alertdialog.builder list shown.

public class inputmydialogfragment      extends sherlockdialogfragment      implements onclicklistener {      private listview listview;     private arrayadapter<string> adapter;     private string[] myitemarray;      @override     public dialog oncreatedialog(bundle savedinstancestate) {         myitemarray = getresources().getstringarray(r.array.my_item_array);         listview = (listview) getactivity().findviewbyid(r.id.dialog_listview);         adapter = new arrayadapter<string>(getactivity(), r.id.row_relativelayout, myitemarray);         listview.setadapter(adapter);          return new alertdialog.builder(getactivity())             .setview(getactivity().getlayoutinflater().inflate(r.layout.dialog, null))             //.setadapter(adapter, this)             .setpositivebutton("ok", new dialoginterface.onclicklistener() {                 @override                 public void onclick(dialoginterface dialog, int which) {                     // todo                 }             })             .setnegativebutton("cancel", new dialoginterface.onclicklistener() {                 @override                 public void onclick(dialoginterface dialog, int which) {                     dialog.dismiss();                 }             }).create();     }      @override     public void onclick(dialoginterface dialog, int which) {         // todo auto-generated method stub     } } 

any suggestions?

edit:

it turns out row-entry xml seems incorrect, since tested below mentioned code

adapter = new arrayadapter<string>(getactivity(), android.r.layout.simple_list_item_single_choice, myitemarray); 

and worked.

here's xml custom row-entry, found in tutorial (can't find link anymore):

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/row_relativelayout"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_centervertical="true" >      <textview         android:id="@+id/row_textview"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:ellipsize="end"         android:gravity="left|center_vertical"         android:singleline="false"         android:textappearance="@android:style/textappearance.devicedefault.small" />      <checkedtextview         android:id="@+id/row_checkstate"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentright="true"         android:layout_centervertical="true"         android:layout_marginright="6dip"         android:background="#00000000"         android:checkmark="@android:drawable/btn_radio"         android:checked="false"         android:clickable="false"         android:focusable="false" />  </relativelayout> 

i have no idea wrong...

you try find yout list before inflating layout.

try this, @ beginning of method :

view v = getactivity().getlayoutinflater().inflate(r.layout.dialog, null); //if have 1 listview in layout, v listview // listview lv = (listview)v; listview lv = v.findbyid(r.id.dialog_listview); 

then, give v instead or inflating layout in return statement.

return new alertdialog.builder(getactivity())         .setview(v)... 

Comments