Ich bin neu bei Android Entwicklung, und ich versuche, eine einfache SharedPreferences
in meiner (bisher) Aktivität zu erstellen. Jedes Mal, wenn ich versuche, es auszuführen, erhalte ich zur Laufzeit eine Null-Zeiger-Ausnahme, obwohl ich die Variable in der Deklarationszeile zugewiesen habe. Meine volle Spur:Android wirft eine NullPointerException beim Instanziieren von SharedPreferences
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{andyszoke.macrohub/andyszoke.macrohub.SetMacroManualActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3242)
at android.app.ActivityThread.access$1000(ActivityThread.java:205)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1695)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6895)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:111)
at andyszoke.macrohub.SetMacroManualActivity.<init>(SetMacroManualActivity.java:17)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1690)
at android.app.Instrumentation.newActivity(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2978)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3242)
at android.app.ActivityThread.access$1000(ActivityThread.java:205)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1695)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6895)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
und die entsprechenden Bits des Codes:
package andyszoke.macrohub;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.WindowDecorActionBar;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SetMacroManualActivity extends AppCompatActivity {
private static final String TAG = "myMessage";
private static final String MY_PREF = "MyPreferences";
SharedPreferences prefs = this.getSharedPreferences(MY_PREF, 0);
SharedPreferences.Editor editor = prefs.edit();
Die Ausnahme wird in der dritten Zeile in der Klasse geworfen. Ich habe alle möglichen Kombinationen von Deklarationen ausprobiert und mir so viele Stackoverflow-Beiträge angesehen, wie ich finden konnte, aber nichts bringt es durcheinander. Jede Hilfe wäre willkommen!
Verschieben Sie die 'prefs'- und' editor'-Initialisierungen nach 'onCreate()'. –