转换共享首选项保存ArrayList JSON字符串回到可用对象抛出活动的空指针?

时间:2016-06-04 15:11:30

标签: java android json listview arraylist

高级环境

每次用户恢复应用程序时,我都试图让ListView更新,当时我注意到Debug中抛出了这个异常。

ArrayList<Flow> FlowManager保存在 SharedPreferences 中  并使用自定义数组适配器(onResume())检索FlowArrayAdapter以填充 listView

捕获抛出的空指针(请参阅调试图片),但我想知道是否有任何想法为什么首先在该行为this抛出NullPointerException?

不是this ==当前的活动(TheStream.Activity),因此是一个对象,因此不是null

抓到异常 enter image description here enter image description here

好奇的代码:

TheStream.java

public class TheStream extends AppCompatActivity {

    private Flow newFlow; //Blank flow object declared.

    private static ArrayList<Flow> flowManager;

    private FlowArrayAdapter helperAdapter;
    private ListView listView;

    /** UI Actions and Set up */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_the_stream);

        listView = (ListView) findViewById(R.id.streamFeed);
    }


    @Override
    protected void onResume() {
        super.onResume();
        populateList();
        setItemOnClicks();

    }

   private void populateList() {

    try {
        Log.d(TAG, "Retrieving flowManager data...");
        SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor prefsEditor = mPrefs.edit();
        Gson gson = new Gson();

        // Convert JSON Object stored as STRING in Shared Prefs, back to
        // Useable ArrayList Collection

        String json = mPrefs.getString("flowManager", null);

               // >>> NULLPOINTER THROWN + CAUGHT HERE 
        Type type = new TypeToken<ArrayList<Flow>>() {}.getType();
              // >>> NULLPOINTER THROWN  + CAUGHT HERE 

        flowManager = gson.fromJson(json, type);

    } catch (Exception e) {
        Log.e(TAG, "Exception throw in populating list: " + e.getMessage());
    }

    if (flowManager==null) {
        flowManager = new ArrayList<Flow>();
    }

    helperAdapter = new FlowArrayAdapter(this, flowManager);
    listView.setAdapter(helperAdapter);

    helperAdapter.notifyDataSetChanged();
}

FlowArrayAdapter.Java

public class FlowArrayAdapter extends ArrayAdapter<Flow> {
private static class ViewHolder {
    TextView name;
    TextView elements;
    TextView timeEstimate;
}

public FlowArrayAdapter(Context context, ArrayList<Flow> Flows) {
    super(context, R.layout.flow_item_in_stream, Flows);
} // End of constructor

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();

        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.flow_item_in_stream, parent, false);


        holder.name = (TextView) convertView.findViewById(R.id.item_flow_name);
        holder.elements = (TextView) convertView.findViewById(R.id.item_element_count);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    // Get the data item for this position
    Flow flow = getItem(position);

    // Populate the data into the template view using the data from Flow object

    holder.name.setText(String.valueOf(flow.getName()));

    holder.elements.setText(String.valueOf(flow.getElementCount()));

    return convertView;
    }

}

1 个答案:

答案 0 :(得分:1)

libfoo.so.1

如果&#34; flowManager&#34;这将返回null。密钥不在共享首选项中找到。因为方法调用中的第二个参数是defualt返回值。因此,如果你想避免空指针在尝试使用&#34; json&#34;之前进行空检查。变量。你需要在ShredPreferences.Editor上调用.apply()才能保存它。示例

libfoo.so.2