【问题标题】:Android: SharedPreferences PreferenceManager.getDefaultSharedPreferences in ClassAndroid:类中的 SharedPreferences PreferenceManager.getDefaultSharedPreferences
【发布时间】:2015-04-16 15:43:08
【问题描述】:

我想从我的偏好片段中获取一个字符串到我的 android 应用程序中的一个类,但我无法像往常那样获取它。

我班上的代码:

public class VtplListContentItem implements VtplListItem  {

    private VtplEntry m_data;       

    public VtplListContentItem(VtplEntry data) {
        m_data = data;
    }

    @Override
    public int getViewType() {
        return RowType.LIST_ITEM.ordinal();
    }

    @Override
    public View getView(LayoutInflater inflater, View convertView) {
        View view;
        if(convertView == null) {
            view = inflater.inflate(R.layout.list_item, null);
        }
        else {
            view = convertView;
        }

        [...]

        // (this) is the error Context cannot be applied to this class
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 

        String myClass = prefs.getString("UserMyClass", "null");

        // Mark
        if (m_data.getSchoolClass().contains(myClass)) {
            view.setBackgroundResource(R.color.accent);
        } else {
            view.setBackgroundResource(android.R.color.white);
        }
        return view;
    }

    @Override
    public VtplEntry getData() {
        return m_data;
    }

}

我试过了:

public class VtplListContentItem extends Activity implements VtplListItem {

然后我得到错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at com.philipp_mandler.android.vtpl.VtplListContentItem.getView(VtplListContentItem.java:52)
        at com.philipp_mandler.android.vtpl.VtplListAdapter.getView(VtplListAdapter.java:38)

第 52 行是:

SharedPreferences 首选项 = PreferenceManager.getDefaultSharedPreferences(this);

问题是(this),但不知道需要选择哪个上下文

【问题讨论】:

    标签: java android class sharedpreferences android-preferences


    【解决方案1】:

    您应该在构造函数中实例化将 Activity 的上下文作为参数传递的对象,然后从那里使用它

    【讨论】:

    • 如何以最好的方式做到这一点? :)
    • VtplListContentItem 您必须构造此对象,将上下文从您实例化的活动中传递出去,例如:class MyActivity extends Activity { VtplListContentItem s = new VtplListContentItem(sampledata, MyActivity.this); //And then you can use the context of the activity you have passed to use in the method you have the shared prefs }
    【解决方案2】:

    将上下文传递给构造函数

    public class VtplListContentItem implements VtplListItem  {
    
        private Context m_context;
    
        public VtplListContentItem(VtplEntry data, Context context) {
            m_data = data;
            m_context = context;
        }
    
        ...
    
        @Override
        public View getView(LayoutInflater inflater, View convertView) {
            ...
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(m_context);
            ...
        }
    
        ...
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多