• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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: 5395
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • 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:

 
Danger, 10,000 volts, very electic .... tiny ad:
ScroogeXHTML - the small and fast RTF to HTML converter library
https://coderanch.com/t/707504/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!