Reputation: 83
I have an Application class, but the OnCreate method is not called on my HTC One m8 (Android 6). On the emulator (Android 5.1) it works fine.
public class App extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
public static Context getContext(){
return context;
}
}
AndroidManifest.xml:
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
How can I fix this?
Upvotes: 2
Views: 888
Reputation: 11
Just check with App class is placed in correct place. If you set name with .APP then it will look that class in com.yourpackage.APP else place correct path in manifest. If all fine then sure onCreate will be called and getApplicationContext() will give context reference.
Upvotes: 1