Array Adapter NotifyDataSetChanged() NullPointerException Android

落花浮王杯 提交于 2020-01-07 02:51:09

问题


I am adding a map to a list and then refreshing the array adapter. This was working perfectly earlier, but now that I am using an addItem() method from two different methods, it throws a NullPointer. I hope my code will clear up what I am saying:

SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView listthings;
int[] to;
String[] from;
String painLevelString, timeOfPainString, textTreatmentString,
        painLocation, row1, row2, name;

OnCreate(){
 if(getIntent().getStringExtra("newPainLevel")!= null){
        createNewEditedEntry();

        }
     adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout,
            from, to);
    listthings.setAdapter(adapter);
}

 private void createNewEditedEntry() {
    String newPainLevel = this.getIntent().getStringExtra("newPainLevel");
    String newPainTime =this.getIntent().getStringExtra("newPainTime");
    String newTreatment =this.getIntent().getStringExtra("newTreatment");
    painLevelString = newPainLevel;
    timeOfPainString = newPainTime;
    textTreatmentString = newTreatment;
    row1 = "sample1";
    row2 = "sample2";

    addItem();
    //painItems.remove(getIntent().getStringExtra("position"));
    //adapter.notifyDataSetChanged();

 }

@Override
// on the activityresult,get the string extra, then add the item to the list
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == 1) {
        row1 = data.getStringExtra("com.painLogger.row1");
        row2 = data.getStringExtra("com.painLogger.row2");


        painLevelString = data.getStringExtra("com.painLogger.painLevel");
        painLocation = data.getStringExtra("painLocation");
        timeOfPainString = data.getStringExtra("com.painLogger.painTime");
        textTreatmentString = data
                .getStringExtra("com.painLogger.treatment");
        addItem();
    }
}

// to add the item, put it in the map, and add the map into the list
private void addItem() {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("row_1", row1);
    map.put("row_2", row2);
    map.put("row_3", painLevelString);
    map.put("row_4", painLocation);
    map.put("row_5", timeOfPainString);
    map.put("row_6",textTreatmentString);
    painItems.add(map);
        adapter.notifyDataSetChanged(); //Null Pointer **HERE**
    }

Just to be clear, createNewEditedEntry() and onActivityResult() are never called at the same time and never clash. They are two completely different occurrences. It was working with just the OnActivityResult, but now when I use CreateNewEditedEntry(), it has stopped working. I have also checked and made sure that none of the Strings that I fetch from my intent are null.


回答1:


You need to initialize adapter before you call createNewEditedEntry in onCreate:

OnCreate(){
     adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout, from, to);
      if(getIntent().getStringExtra("newPainLevel")!= null){
        createNewEditedEntry();    
      }
// ...
}

Otherwise, adapter will be null in addItem called from there (last line of createNewEditedEntry).




回答2:


You should also be aware that your call to addItem can fail if your activity was destroyed by Android while you were in the sub-activity. Have a read of my answer to this question to understand what can happen when resources get low.



来源:https://stackoverflow.com/questions/6894613/array-adapter-notifydatasetchanged-nullpointerexception-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!