2

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?

Yaseen Ahmad
  • 1,807
  • 5
  • 25
  • 43
tomhogenkamp
  • 83
  • 1
  • 11

1 Answers1

1

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.

Yuvisiva
  • 11
  • 5