ANDROID JAVA:当使用的SharedPreferences时,Activity不会启动

时间:2021-12-29 01:31:59

I have a problem. I want to read value Integer of activity "Settings" in the service GPSTracker and use it there. I use this SharedPrefrences and confirming key input. If the data is validated that the application returns to class FullscreenActivity. This is the code responsible for this in activity Settings:

我有个问题。我想在服务GPSTracker中读取活动“设置”的值Integer并在那里使用它。我使用此SharedPrefrences并确认键输入。如果数据已验证应用程序返回到FullscreenActivity类。这是活动设置中负责此操作的代码:

    SharedPreferences.Editor editor;
    public static final String NAME = "DISTANCE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content);

    SharedPreferences pref = getApplicationContext().getSharedPreferences(NAME, MODE_PRIVATE);
    editor=pref.edit();

    Edit1= (EditText) findViewById(R.id.editSkan);
    accept= (Button) findViewById(R.id.buttonSkan);
    accept.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (!Edit1.getText().toString().equals(""))
                value = Integer.parseInt(Edit1.getText().toString());
            editor.putInt("settings", value);
            editor.commit();

            Toast.makeText(getApplicationContext(), "Changed, value + " m", Toast.LENGTH_SHORT).show();
            finish();
        }
    });

A code in GPSTracker like this:

GPSTracker中的代码如下:

      SharedPreferences pref;

(...)

    private int downloadSettings()
    {
        pref=context.getSharedPreferences("DISTANCE", Activity.MODE_PRIVATE);
        value = pref.getInt("settings",15); 

        return value;
    }

and a method call:

和方法调用:

    int dist = downloadSettings();

When I run the apps I get two errors in the log:

当我运行应用程序时,我在日志中遇到两个错误:

*java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adam.mobileproject/com.example.adam.mobileproject.FullscreenActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.adam.mobileproject.GPSTracker.downloadSettings(GPSTracker.java:284)
            at com.example.adam.mobileproject.GPSTracker.<init>(GPSTracker.java:200)
            at com.example.adam.mobileproject.FullscreenActivity.onCreate(FullscreenActivity.java:40)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)*

PLEASE HELP!!!

1 个解决方案

#1


0  

The problem is definitely that context is null.

问题肯定是上下文为空。

However, just like the Activity class, the Service class extends Context, so you should be able to replace context with this or getApplicationContext().

但是,就像Activity类一样,Service类扩展了Context,因此您应该能够用this或getApplicationContext()替换context。

Try this:

private int downloadSettings()
{
    pref = getApplicationContext().getSharedPreferences("DISTANCE", Activity.MODE_PRIVATE);
    value = pref.getInt("settings",15); 

    return value;
}

#1


0  

The problem is definitely that context is null.

问题肯定是上下文为空。

However, just like the Activity class, the Service class extends Context, so you should be able to replace context with this or getApplicationContext().

但是,就像Activity类一样,Service类扩展了Context,因此您应该能够用this或getApplicationContext()替换context。

Try this:

private int downloadSettings()
{
    pref = getApplicationContext().getSharedPreferences("DISTANCE", Activity.MODE_PRIVATE);
    value = pref.getInt("settings",15); 

    return value;
}