Custom Dialog with a listview crashing-Collection of common programming errors

I’ve tried different things but things are still not ‘going’

I have an activity. In this, when I press one of the options in the menu, I need to popup a dialog and in that dialog a listview of string values wich I get out of an string-arrays xml.

I’ve already deleted some code to clean it up but still no luck…

myList.xml



my custom dialog

public class Selector extends Dialog {

String[] testArray;
protected Selector(Context context) {
    super(context);

    ListView lijstje = (ListView) findViewById(R.layout.mylist);
    testArray = context.getResources().getStringArray(R.array.currencies);
    Log.d("test",testArray[0]);//wich shows me the 1e string and it's working
    ArrayAdapter adapter = new ArrayAdapter(this.getContext(),android.R.layout.simple_list_item_1, testArray);
    lijstje.setAdapter(adapter);

    LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = li.inflate(R.layout.mylist, null, false);
    this.setContentView(v);

}

}

in my main activity I have I function

public void showPopup()
{       
    Selector test = new Selector(this);
    test.show();
}

when I put lijstje.setAdapter(adapter); in comment my app is not crashing but the dialog is very small and empty

02-24 22:14:29.705: D/AndroidRuntime(28734): Shutting down VM
02-24 22:14:29.705: W/dalvikvm(28734): threadid=1: thread exiting with uncaught exception (group=0x40a4e1f8)
02-24 22:14:29.715: E/AndroidRuntime(28734): FATAL EXCEPTION: main
02-24 22:14:29.715: E/AndroidRuntime(28734): java.lang.NullPointerException
02-24 22:14:29.715: E/AndroidRuntime(28734):    at be.veeteedev.OmzetterActivity.showPopup(OmzetterActivity.java:83)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at be.veeteedev.OmzetterActivity.onOptionsItemSelected(OmzetterActivity.java:59)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.app.Activity.onMenuItemSelected(Activity.java:2502)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.widget.AbsListView$1.run(AbsListView.java:3168)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.os.Handler.handleCallback(Handler.java:605)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.os.Looper.loop(Looper.java:137)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.app.ActivityThread.main(ActivityThread.java:4424)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at java.lang.reflect.Method.invokeNative(Native Method)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at java.lang.reflect.Method.invoke(Method.java:511)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at dalvik.system.NativeStart.main(Native Method)
  1. I’m not sure this will magically solve all you’re problems, but in your Selector constructor you have the following assignment:

    ListView lijstje = (ListView) findViewById(R.layout.mylist);
    

    Note how you are trying to find a View with a layout id. It should say something like R.id.mylist in stead. Currently it’ll resolve to null, hence the NullPointerException.

    Also, you’re doing setContentView(...) all the way at the end. If you’re trying to inflate views from the mentioned layout file (R.layout.mylist), make sure you do this after having set the content view.

    By the way, if you don’t have a fancy layout for the dialog displaying a list, you can probably suffice with the default AlertDialog builder. Have a read here, samples included. For some help on custom dialogs, look further down the bottom of that same page.

  2. I’m guessing that this is your problem. There is nothing to find the view by

    ListView lijstje = (ListView) findViewById(R.layout.mylist);
    

    lijstje is null when called this way lijstje is a layout not a view

    also set content view should be before anything else

    public class Selector extends Dialog {
    
    String[] testArray;
    protected Selector(Context context) {
        super(context);
        setContentView(R.layout.list);
    
        ListView lijstje = (ListView) findViewById(R.id.mylist);
        testArray = context.getResources().getStringArray(R.array.currencies);
        Log.d("test",testArray[0]);//wich shows me the 1e string and it's working
        ArrayAdapter adapter = new ArrayAdapter (this.getContext(),android.R.layout.simple_list_item_1, testArray);
        lijstje.setAdapter(adapter);
    }
    

    mylist.xml

    
    
        
    
    
    
  3. check the following links dialogwithlistview stackoverflow anddev