I´m trying to use a global variable. I got this doing the next:
1/ First I create this class
package ar.ncantarini.mapa;
import android.app.Application;
public class MyApplication extends Application {
int id_mascota;
public int getId_mascota() {
return id_mascota;
}
public void setId_mascota(int id_mascota) {
this.id_mascota = id_mascota;
}
}
2/ In some Activity I put:
// Calling Application class (see application tag in AndroidManifest.xml)
final MyApplication globalVariable = (MyApplication) getApplicationContext();
//Set name and email in global/application context
globalVariable.setId_mascota(1);
3/ In other Activity I do:
// Calling Application class (see application tag in AndroidManifest.xml)
final MyApplication globalVariable = (MyApplication) getApplicationContext();
// Get name and email from global/application context
final int name = globalVariable.getId_mascota();
//inside a method
4/ At last, In the Manifest I add the android name tag.
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light.NoActionBar" >
<activity
Every thing works well. But my problem is that now I need to move the "MyApplication" class to another packcage calls "Utilities" which is not the "main" packcage of the android app.
After move this class, I have test change the manifest like that:
1_android:name="Utilities.MyApplication"
2_android:name="Utilities$MyApplication"
3_android:name=".Utilities.MyApplication"
And always I have the " no source find" error. The same problem is in another project when a use de "BroadcasteReceiver" class, and when I put this in a packcage that isnt the main packcage. In my case my main packcage is ar.ncantarini.mapa.
EDIT: here I add a photo of the error I always have, when I move the MyApplication class to the Utilities packcage .
At your manifest, starting a declaration with a dot (.) you are referencing the main package, you can also declare the full name of your Activity class.
In your case, its just:
android:name="Utilities.MyApplication"
Please note, that package names is encouraged to be full lowercase.
Considering the @MarcosVasconcelos answers, I combine them, and I arrived to a solution. The key is that the name of the package in Android should be in lowercase. that´s all.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.