-3

我正在创建一个列表,该列表使用共享首选项将值存储并保留为字符串。我想存储事件的时间(某些事件)。

我的代码看起来像

package com.defcomdevs.invento16;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.util.Log;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

/**
 * Created by midhun on 18/11/15.
 */
public class SetTimings {

    // context to hold the calling activity
    static Context c;
    String[] event_times;
    // declare file name and mode of access
    public static String MY_PREFS = "MY_PREFS";
    private SharedPreferences mySharedPreferences = new SharedPreferences() {
        @Override
        public Map<String, ?> getAll() {
            return null;
        }

        @Nullable
        @Override
        public String getString(String key, String defValue) {
            return null;
        }

        @Nullable
        @Override
        public Set<String> getStringSet(String key, Set<String> defValues) {
            return null;
        }

        @Override
        public int getInt(String key, int defValue) {
            return 0;
        }

        @Override
        public long getLong(String key, long defValue) {
            return 0;
        }

        @Override
        public float getFloat(String key, float defValue) {
            return 0;
        }

        @Override
        public boolean getBoolean(String key, boolean defValue) {
            return false;
        }

        @Override
        public boolean contains(String key) {
            return false;
        }

        @Override
        public Editor edit() {
            return null;
        }

        @Override
        public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {

        }

        @Override
        public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {

        }
    };
    int prefMode = c.MODE_PRIVATE;
    public static String[] eventslist = new String[50];

    public SetTimings(Context context, String[] strings) {

        int i = 0;

        Log.d("Midhun", strings[1]);
        this.c = context;
        eventslist = c.getResources().getStringArray(R.array.Items);
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putString(eventslist[i++], strings[i++]);
        editor.putString(eventslist[i++], strings[i++]);
        editor.putString(eventslist[i++], strings[i++]);
        editor.putString(eventslist[i++], strings[i++]);
        editor.putString(eventslist[i++], strings[i++]);
        editor.putString(eventslist[i++], strings[i++]);
        editor.putString(eventslist[i++], strings[i++]);
        editor.putString(eventslist[i++], strings[i++]);
        editor.putString(eventslist[i++], strings[i++]);
        editor.putString(eventslist[i++], strings[i++]);
        editor.apply();
        Log.d("Midhun", mySharedPreferences.getString(eventslist[2], "null"));
    }

    public String[] getTimings(Context c1) {

        eventslist = c1.getResources().getStringArray(R.array.Items);
        for (int i = 0; i < eventslist.length; i++) {
            event_times[i] = String.valueOf(mySharedPreferences.getString(eventslist[i], ""));
        }
        return event_times;
    }
}

我这样调用类和构造函数:

setTimings= new SetTimings(this,this.getResources().getStringArray(R.array.Items));

并像这样访问字符串:

String[] time= setTimings.getTimings(this);

问题当我运行我的应用程序时它崩溃了。

日志报告:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] com.defcomdevs.invento16.SetTimings.getTimings(android.content.Context)' on a null object reference

这表明错误在getTimings()方法中,我尝试了一些关于 SO 的问题,但对我来说没有成功。

请突出我的错误。并请提供解决方案。谢谢

编辑 我使用的代码setTimings

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this;
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinator);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setLogo(R.mipmap.ic_launcher);
    setSupportActionBar(toolbar);
    getTheAccountName=new GetTheAccountName();
    final Context context=this;
    adapter = new MyAdapter(this, getdata());
    recyclerView = (RecyclerView) findViewById(R.id.recycler);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new GridLayoutManager(this, 2));



    mcollapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    mcollapsingToolbar.setTitle("INVENTO '16");
    mcollapsingToolbar.setExpandedTitleColor(getResources().getColor(R.color.transparent));
    mcollapsingToolbar.setCollapsedTitleTextColor(getResources().getColor(R.color.white));

    fab_menu_titlescreen= (FloatingActionMenu) findViewById(R.id.menu_item);
    fab_map = (FloatingActionButton) findViewById(R.id.map);
    fab_register = (FloatingActionButton) findViewById(R.id.register);
    fab_reminder = (FloatingActionButton) findViewById(R.id.reminder);
    fab_refresh = (FloatingActionButton) findViewById(R.id.refresh);


    String userName=getTheAccountName.getUsername(context);
    String userId=getTheAccountName.getuserId(context);
    //username= (TextView) findViewById(R.id.userNameText);
    //username.setText(userName);
    userid= (TextView) findViewById(R.id.userNameText);
    userid.setText(userId);

    Log.d("Midhun", "before");
    String[] t= this.getResources().getStringArray(R.array.time);
    //pass the time parameters to the class
    setTimings= new SetTimings(this,t);   //used setTimings

    Log.d("Midhun","after");

并在这里

public List<ListItems> getdata() {
    List<ListItems> grid_data = new ArrayList<>();
    int[] icons = {R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100};
    String[] texts = getResources().getStringArray(R.array.Items);
    String[] time= setTimings(context);
    Log.d("Midhun",time[1]);
    for (int i = 0; i < icons.length && i < texts.length; i++) {
        ListItems current = new ListItems();
        current.iconId = icons[i];
        current.IconName = texts[i];
        current.ItemTime = time[i];
        grid_data.add(current);
    }
    return grid_data;
}
4

3 回答 3

1

它与以下行有关

editor.putString(eventslist[i++], strings[i++]);

在这里,您正在执行 i++ 两次。这意味着您没有引用正确的数组元素并因此超出范围

您可能应该使用 for 循环来执行上述操作。实际上,您可以将整个数组“保存”到 sharedpreferences

于 2015-11-19T06:33:15.280 回答
0

由于它在 getTimings() 中抛出 NullPointerException,因此您应该寻找一个为 null 的对象,并且它的方法正在被调用。因此,候选人可能是:c1(上下文类型)、eventListmySharedPreference

于 2015-11-19T06:33:53.613 回答
0

感谢迈克 M。

问题

1.首先是我的适配器实例化中的getdata()是在实例化setTimings之前完成的,所以在那个方法中它是空的。

2.其次,我将字符串数组分配给未初始化的字符串数组eventslist

3.正如William Ku所指出的,我两次使用i++,因此超出了界限。

于 2015-11-19T17:29:06.533 回答