This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Problems trying to set a ListView inside a OnClickListener using AlertDialog  RSS feed

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi, everybody. I'm new in Android and after doing some programming I'm facing this problem. In line 125 of my code in MainActivityAlertDialog where I set adapter for the listview  lvInternalStorage.setAdapter(adapter); I get this error:
MainActivityAlertDialog: checkInternalStorage: NULLPOINTEREXCEPTION Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
After googling I've found this error is because of line 124 of MainActivityAlertDialog  ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivityAlertDialog.this, android.R.layout.simple_list_item_1, FilePathStrings);
where anonymous onclicklistener cannot be converted to context , so If this is the case. What should I have to do?

I'll appreciate any help.







 
Saloon Keeper
Posts: 5229
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The important part is

Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference



So "lyInternalStorage" is null. Start by checking after line 51 that it isn't. You posted two layout files, but "lyInternalStorage" is used only in the second - is the "activity_main" layout maybe the first one that doesn't contain it?

anonymous onclicklistener cannot be converted to context


How do you figure? It doesn't say anything about that anywhere.

FileNameStrings


One last thing: Don't use uppercase names for variables. In Java, class names always start with an uppercase letter, but variable names never do. Not following this convention will confuse anyone looking at your code.
 
joe encalada
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Tim thanks for your suggestions. A solve my problem after taken out alertDialog = new AlertDialog.Builder(MainActivityAlertDialog.this); of mostrarPopupRutas and putting it in onCreate method here below the modified code:

 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!