Ich habe eine Shopper
Klasse erstellt, die Application
Klasse für mein Projekt erweitert. Dies ist, wie ich versuche, Kontext in der KlassegetApplicationContext() liefert null in der Anwendungsklasse
public class Shopper extends Application {
private Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
public Context getContext() {
return context;
}
}
Aber getApplicationContext
immer null
zurück zu bekommen. Fehle ich etwas? Ich habe mir this und this angesehen, um eine Idee zu bekommen, wie es geht; aber immer noch das gleiche Ergebnis.
Ich habe den Namen der Klasse zum Manifest hinzugefügt.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vn.com.shopper">
<application
android:name=".Shopper"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/ShopperTheme"
android:fullBackupContent="true">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
EDIT
mich korrigieren, wenn ich falsch bin (ich könnte wahrscheinlich sein), aber ich verstehe nicht, wie das Feld mit context
als statische den Wert von getApplicationContext
beeinflussen können (das ist, was die meisten Antworten weisen darauf hin).
Ich habe das versucht. Funktioniert nicht. Gleiches Ergebnis. –
Warum brauchen Sie einen solchen Kontext? Der Kontext ist in allen Ihren Aktivitäten verfügbar, wenn Sie die Anwendungsklasse in Manifest definiert haben. –
try 'context = this.getApplicationContext();' –