首页 \ 问答 \ 带有context.getSharedPreferences的java.lang.NullPointerException(STORAGE_NAME,0)(java.lang.NullPointerException with context.getSharedPreferences(STORAGE_NAME, 0))

带有context.getSharedPreferences的java.lang.NullPointerException(STORAGE_NAME,0)(java.lang.NullPointerException with context.getSharedPreferences(STORAGE_NAME, 0))

我创建了一个使用SharedPreferences的类。 在我的Activity ,我正在尝试添加项目并获得以下错误...

06-05 17:01:53.950: E/AndroidRuntime(3488): FATAL EXCEPTION: main
06-05 17:01:53.950: E/AndroidRuntime(3488): java.lang.NullPointerException
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.helpers.Prefs.init(Prefs.java:16)
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.helpers.Prefs.addStringProperty(Prefs.java:30)
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.usersadapter.SplashActivity$1.callback(SplashActivity.java:78)
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.usersadapter.SplashActivity$1.callback(SplashActivity.java:1)

这是我处理SharedPreferences代码......

import android.content.Context;
import android.content.SharedPreferences;

public class Prefs {

    public static final String STORAGE_NAME = "ApplicationPrefs";

    private static SharedPreferences settings = null;
    private static SharedPreferences.Editor editor = null;
    private static Context context = null;

    public static void init(Context cntxt) {
        context = cntxt;
    }

    private static void init() {
        settings = context.getSharedPreferences(STORAGE_NAME, 0);
        editor = settings.edit();
    }

    /* String values */
    public static void addStringProperty(String name, String value) {
        if (settings == null || editor == null) {
            init();
        }
        editor.putString(name, value);
        editor.commit();
    }

    public static String getStringProperty(String name) {
        if (settings == null || editor == null) {
            init();
        }
        return settings.getString(name, null);
    }
}

这是我在我的Activity调用的代码...

Prefs.addStringProperty("client_id", "1JDkv9sdfj8sf63rjs");

I have created a class for working with SharedPreferences. In my Activity, I am trying to add an item and get the following error...

06-05 17:01:53.950: E/AndroidRuntime(3488): FATAL EXCEPTION: main
06-05 17:01:53.950: E/AndroidRuntime(3488): java.lang.NullPointerException
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.helpers.Prefs.init(Prefs.java:16)
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.helpers.Prefs.addStringProperty(Prefs.java:30)
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.usersadapter.SplashActivity$1.callback(SplashActivity.java:78)
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.usersadapter.SplashActivity$1.callback(SplashActivity.java:1)

This is my code for handling the SharedPreferences...

import android.content.Context;
import android.content.SharedPreferences;

public class Prefs {

    public static final String STORAGE_NAME = "ApplicationPrefs";

    private static SharedPreferences settings = null;
    private static SharedPreferences.Editor editor = null;
    private static Context context = null;

    public static void init(Context cntxt) {
        context = cntxt;
    }

    private static void init() {
        settings = context.getSharedPreferences(STORAGE_NAME, 0);
        editor = settings.edit();
    }

    /* String values */
    public static void addStringProperty(String name, String value) {
        if (settings == null || editor == null) {
            init();
        }
        editor.putString(name, value);
        editor.commit();
    }

    public static String getStringProperty(String name) {
        if (settings == null || editor == null) {
            init();
        }
        return settings.getString(name, null);
    }
}

This is the code I am calling in my Activity...

Prefs.addStringProperty("client_id", "1JDkv9sdfj8sf63rjs");

原文:https://stackoverflow.com/questions/10898221
更新时间:2022-04-08 06:04

最满意答案

好吧,如果这不起作用,并且你与设计结婚,我认为你必须使用bg图像。 我找不到用CSS设置滚动条样式的方法。 我用这个解决方案演示了另一个jsfiddle: http//jsfiddle.net/jlmyers42/mrx46geg/

但基本上,你只需移动一些CSS:

#sb_body {
  width: 272px;
  height: 300px;
  overflow: auto;
  padding: 0;
  margin: 0;
  background: url("http://arcsuviam.com/play/random/bluebg.png") no-repeat left top;
}
div#sb_messages_set {
  margin: 5px;
}
div.sb_message {
  padding: 2px 4px 5px 4px;
  border-bottom-style: dashed;
  border-width: 1px;
  border-color: black;
}

Well, if that won't work, and you're married to the design, I think you have to use a bg image. I can't find a way to style the scrollbar with CSS. I made another jsfiddle with this solution demonstrated: http://jsfiddle.net/jlmyers42/mrx46geg/

But basically, you just move some of your CSS around:

#sb_body {
  width: 272px;
  height: 300px;
  overflow: auto;
  padding: 0;
  margin: 0;
  background: url("http://arcsuviam.com/play/random/bluebg.png") no-repeat left top;
}
div#sb_messages_set {
  margin: 5px;
}
div.sb_message {
  padding: 2px 4px 5px 4px;
  border-bottom-style: dashed;
  border-width: 1px;
  border-color: black;
}

相关问答

更多
  • 我会创建一个固定大小的DIV,将其放入body (获得100%宽度和100%高度),将所有内容放入该DIV并将其overflow-y : scroll或auto I would create a fixed-sized DIV, place that into the body (which gets 100% width and 100% height), put all content into that DIV and give it overflow-y : scroll or auto
  • .tree-table-view .filler {} 是我需要的选择器吗? 谢谢JNS! .tree-table-view .filler { } Was the selector I needed. Thanks JNS!
  • 我相信你所能做的只是添加一个背景图像,就像这样 .mydiv::-webkit-scrollbar-button:vertical { background: url('https://pixabay.com/static/uploads/photo/2015/10/07/10/55/arrow-975992_960_720.png') no-repeat center center; background-size:cover; } 这是一个小提琴 和一些源材料进行了广泛的研究 I be ...
  • 问题是你无法将div2修复为div1,因为修复div意味着“保持div的顶部,从最近的定位容器的内顶部测量,恒定”。 而在内部顶部我指的是容器的上部,当用户滚动时,它会上下移动。 解决方案( 这里是jsfiddle )将div1和div2都固定到外部容器。 这将是你的CSS: