我是Android开发的新手,我尝试在我的(目前为止)活动中创建一个简单的SharedPreferences
。但是,每次我尝试运行它时,即使我在声明行上分配变量,我也会在运行时获得Null-Pointer异常。我的全部记录:
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)
以及相关的代码:
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();
在类的第三行抛出异常。我已经尝试了各种不同的声明组合,并查看了尽可能多的stackoverflow帖子,但没有任何东西可以解决它。任何帮助将不胜感激!
答案 0 :(得分:1)
用于设置共享首选项的代码在onCreate之前运行。出于所有常规目的,只应在onCreate之前分配变量。当你说过这个.getSharedPreferences时,这个词就是这个&#39;是指活动,但尚未调用onCreate(因此活动尚未形成)。所以&#39;这个&#39;被分配给空对象。所以获得了NullPointerException。
public class SetMacroManualActivity extends AppCompatActivity {
private static final String TAG = "myMessage";
private static final String MY_PREF = "MyPreferences";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// load the layout
setContentView(R.layout.filters);
SharedPreferences prefs = getSharedPreferences(MY_PREF, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
MODE_PRIVATE是通常使用的,还有其他基于要求的模式。所以请检查一下对你来说没问题。