Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

global varible gives ClassCastException

 
sam liya
Ranch Hand
Posts: 1240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my main class



this is manifest file



below is the error i am getting

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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: 42968
73
  • Mark post as helpful
  • send pies
  • 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.
 
sam liya
Ranch Hand
Posts: 1240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i have added the below to manifest

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
sam liya
Ranch Hand
Posts: 1240
  • Mark post as helpful
  • send pies
  • 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: 42968
73
  • Mark post as helpful
  • send pies
  • 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.
 
sam liya
Ranch Hand
Posts: 1240
  • Mark post as helpful
  • send pies
  • 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: 42968
73
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic