• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

global varible gives ClassCastException

 
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is my main class



this is manifest file



below is the error i am getting

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something is amiss: the stack trace mentions line 93 in the onCreate method of MainActivity, but the code you posted is way shorter. Which one is line 93?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, that stack trace makes more sense. You're not setting the app context class to anything called "Global" in the manifest, so it's android.app.Application - nothing mysterious about it.
 
shawn peter
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But i have added the below to manifest

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sameera liyanage wrote:But i have added the below to manifest


Yes, you defined an Activity with the name Global. But you try to get the application (Global global = ((Global)getApplicationContext());) and assign it to Global. The Application Context is a subclass of android.app.Application, not an activity.

Read this document on the Android Manifest: http://developer.android.com/guide/topics/manifest/manifest-intro.html. It has links on the application element, which is what you would use to override the class used for the Application Context.
 
shawn peter
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I change the manifest code as below.Now it's fine.Anyway if i have two Global classes how to add them to manifest file.Because we can't add two name parameters to manifest file

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An app can only have a single app context class. What are you trying to accomplish by having two? That would seem to make things more difficult, instead of simpler.
 
shawn peter
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
below is my Global class


My problem is ,in the set method it just initialize he size of arra.it's ok.But how to add element to them?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can either call getTIME() (for example), and then do whatever needs doing with the array it returns, or add a method that addToTIME(String) that adds that string as the last element of the TIME array.

Two remarks, though: variables have lowercase names. While uppercase is permissible, it is so exceedingly rare that people who look at your code will wonder what's going on. Uppercase is generally used for names of constants.

Secondly, if you don't know beforehand how many elements an array has, it's probably better to use a Collection instead of an array - maybe an ArrayList<String>. That makes it easy to add elements at the end, and to find out how many elements it already contains.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic