定义类 demoapp , 结果 activity 调用始终报类错郁闷呀!
class demoapp extends application{
}
下面的配置注意:
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true"
android:name=".demoapp">
android:debuggable="true" 允许调试的意思
firstly, you should create a new class that will behave as our global object, for example :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<code>package</code>
<code>com.jameselsey.domain;</code>
<code>import</code>
<code>java.util.arraylist;</code>
<code>java.util.list;</code>
<code>com.google.android.maps.geopoint;</code>
<code>android.app.application;</code>
<code>/**</code>
<code>* this is a global pojo that we attach data to which we</code>
<code>* want to use across the application</code>
<code>* @author james elsey</code>
<code>*</code>
<code>*/</code>
<code>public</code>
<code>class</code> <code>globalstate</code><code>extends</code>
<code>application</code>
<code>{</code>
<code>private</code>
<code>string testme;</code>
<code>string gettestme() {</code>
<code>return</code>
<code>testme;</code>
<code>}</code>
<code>void</code> <code>settestme(string testme) {</code>
<code>this</code><code>.testme = testme;</code>
the above class declares one string, with accessor methods. soon we will see how this object will be available throughout the application. you can add anything you want onto this object, in my application i have a list of other domain classes that i wanted
to use elsewhere in my application.
right, now that you have your global class defined, you need to specify this in your androidmanifest file, otherwise the application won’t know where to find this class, and you will get a classnotfoundexception
locate your application tag, and add this into it :
<code>android:name=</code><code>"com.jameselsey.domain.globalstate"</code>
it must reference your fully qualified global state class
now you have it set, your ready to start putting data in, and pulling data out, use the following
<code>// set values</code>
<code>globalstate gs = (globalstate) getapplication();</code>
<code>gs.settestme(</code><code>"some string"</code><code>);</code></code>
<code>// get values</code>
<code>string s = gs.gettestme();</code>
and thats it! off you go!