Android App crashes after Scrolling down ListView

我的梦境 提交于 2019-12-20 06:27:21

问题


I have a listView, everything's working fine whan I don't have too much items on it.. When the list of items is long when I scroll down it crashes at a certain point...

This is my adapter code :

public class SearchListViewAdapter extends BaseAdapter {

private LayoutInflater layoutInflater;
private JsonArray searchResults;

public SearchListViewAdapter(Context context, JsonArray searchResults) {
    this.searchResults = searchResults;
    layoutInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return searchResults.count();
}

@Override
public Object getItem(int position) {
    return searchResults.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    JsonObject searchResult = (JsonObject)getItem (position);
    ViewHolder holder = null;

    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.custom_search_result, null);
        holder = new ViewHolder();
        holder.txtFullName = (TextView) convertView.findViewById(R.id.FullName);
        holder.txtDateScan = (TextView) convertView.findViewById(R.id.DateScan);
        holder.txtBarcodeKeyword = (TextView) convertView.findViewById(R.id.BarcodeKeyword);
        holder.txtHourScan = (TextView) convertView.findViewById(R.id.HourScan);
        holder.imgScanStatus = (ImageView) convertView.findViewById(R.id.ScanStatus);

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

    holder.txtFullName.setText(searchResult.getString ("FirstName") + searchResult.getString ("LastName"));
    holder.txtBarcodeKeyword.setText(searchResult.getString("Barcode"));

    JsonObject ScanInfo = null;
    try {
        ScanInfo = searchResult.getObject("ScanInfo");
    } catch (JsonException e) {
        e.printStackTrace();
    }
    String LastScanTime[] = ScanInfo.getString("LastScanTime").split(" ");

    holder.txtDateScan.setText(LastScanTime[0]);

    try {
        if(ScanInfo.getInt("Scans") == 0 || StringUtils.isNullOrEmpty(ScanInfo.getString("Scans")))
            holder.imgScanStatus.setImageResource(R.drawable.red);
        else if(ScanInfo.getInt("Scans") % 2 == 0)
            holder.imgScanStatus.setImageResource(R.drawable.orange);
        else if(ScanInfo.getInt("Scans") % 2 == 1)
            holder.imgScanStatus.setImageResource(R.drawable.green);
    } catch (JsonException e) {
        e.printStackTrace();
    }

    holder.txtHourScan.setText(LastScanTime[1]);

    return convertView;
}

static class ViewHolder {
    TextView txtFullName;
    TextView txtDateScan;
    TextView txtBarcodeKeyword;
    TextView txtHourScan;
    ImageView imgScanStatus;
}
}

and the Logcat :

09-22 16:35:31.191: E/AndroidRuntime(31440): FATAL EXCEPTION: main
09-22 16:35:31.191: E/AndroidRuntime(31440): java.lang.NullPointerException
09-22 16:35:31.191: E/AndroidRuntime(31440):    at com.eventpulse.app.SearchListViewAdapter.getView(SearchListViewAdapter.java:72)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.widget.AbsListView.obtainView(AbsListView.java:2255)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.widget.ListView.makeAndAddView(ListView.java:1769)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.widget.ListView.fillDown(ListView.java:672)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.widget.ListView.fillGap(ListView.java:636)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5040)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4197)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.view.Choreographer.doFrame(Choreographer.java:524)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at   android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.os.Handler.handleCallback(Handler.java:615)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.os.Looper.loop(Looper.java:137)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at android.app.ActivityThread.main(ActivityThread.java:4745)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at java.lang.reflect.Method.invokeNative(Native Method)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at java.lang.reflect.Method.invoke(Method.java:511)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 16:35:31.191: E/AndroidRuntime(31440):    at dalvik.system.NativeStart.main(Native Method)

Could you help me please, it's goind to drive me crazy!

Thank you.

EDIT : line 72 : String LastScanTime[] = ScanInfo.getString("LastScanTime").split(" ");

It may be something related to this post : Android ListView throwing NullPointerException when I try to scroll


回答1:


The problem is with you Json code any of json object is being null you shall take care with this and check for the values of null before applying

You shall do by checking the null valuse

if(mJsonObject!=null)
{
// Then perform you task
}

Your code check at suspected location for null like ScanInfo or LastScanTime

  holder.txtFullName.setText(searchResult.getString ("FirstName") + searchResult.getString ("LastName"));
    holder.txtBarcodeKeyword.setText(searchResult.getString("Barcode"));

    JsonObject ScanInfo = null;
    try {
        ScanInfo = searchResult.getObject("ScanInfo");
    } catch (JsonException e) {
        e.printStackTrace();
    }
    String LastScanTime[] = ScanInfo.getString("LastScanTime").split(" "); // May be here its not getting values and unable to split the content

    holder.txtDateScan.setText(LastScanTime[0]); // if thats false may be LastScanTime is null
    try {
        if(ScanInfo.getInt("Scans") == 0 || StringUtils.isNullOrEmpty(ScanInfo.getString("Scans")))
            holder.imgScanStatus.setImageResource(R.drawable.red);
        else if(ScanInfo.getInt("Scans") % 2 == 0)
            holder.imgScanStatus.setImageResource(R.drawable.orange);
        else if(ScanInfo.getInt("Scans") % 2 == 1)
            holder.imgScanStatus.setImageResource(R.drawable.green);
    } catch (JsonException e) {
        e.printStackTrace();
    }

    holder.txtHourScan.setText(LastScanTime[1]);


来源:https://stackoverflow.com/questions/18945623/android-app-crashes-after-scrolling-down-listview

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