List view crashing

后端 未结 5 1916
迷失自我
迷失自我 2021-01-06 12:48

I am trying to create ListView and fill it with data , but when i launch the application crashes and shows me a message

Unfortunately,…

相关标签:
5条回答
  • 2021-01-06 13:03

    Change:

    ArrayAdapter<String> value = new ArrayAdapter<String>(this,
           android.R.layout.simple_list_item_1, R.id.mylist, actions);
    

    To this:

    ArrayAdapter<String> value = new ArrayAdapter<String>(this,
           android.R.layout.simple_list_item_1, android.R.id.text1, actions);
    
    //OR
    
    ArrayAdapter<String> value = new ArrayAdapter<String>(this,
           android.R.id.text1, actions);
    
    0 讨论(0)
  • 2021-01-06 13:05

    It seems that your misused its constructor.

    Try this:

    ArrayAdapter<String> value = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, actions);
    

    In ArrayAdapter Documentaion, the third parameter should be an ID of a TextView

    0 讨论(0)
  • 2021-01-06 13:08

    Your constructor for the arrayadapter is incorrect.

    ArrayAdapter(this,android.R.layout.simple_list_item_1, R.id.mylist, actions);

    I believe R.id.mylist is the identifier for the listview? With this constructor it is actually expecting the identifier of the textview that is part of the layout android.R.layout.simple_list_item_1

    0 讨论(0)
  • 2021-01-06 13:08

    It seem that your layout is out of scope of the code and thus cannot be loaded. When you check the log you can see the error "NullPointerException" which indicates that you are trying to access an object that does not exist.

    Probably it is the line

    ListView  v = (ListView)findViewById(R.id.mylist);
    

    that is not creating the object as you expect. If you put a breakpoint on this line you will see that is is null and by stepping to the next line

    v.setTextFilterEnabled(true);
    

    you will see that the exception occurs here.

    Please check your references and how you invoke the layout in order to get the proper object loading.

    0 讨论(0)
  • 2021-01-06 13:21

    You are getting NullPointerException because of your ArrayAdapter constructor , the third parameter should be the TextView inside the layout (2nd parameter) , change it to android.R.id.text1 and try again.

    0 讨论(0)
提交回复
热议问题