Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向下滚动ListView后Android应用程序崩溃_Java_Android_Android Listview_Adapter - Fatal编程技术网

Java 向下滚动ListView后Android应用程序崩溃

Java 向下滚动ListView后Android应用程序崩溃,java,android,android-listview,adapter,Java,Android,Android Listview,Adapter,我有一个列表视图,一切正常,但我没有太多的项目。。当我向下滚动项目列表很长时,它会在某个点崩溃 这是我的适配器代码: public class SearchListViewAdapter extends BaseAdapter { private LayoutInflater layoutInflater; private JsonArray searchResults; public SearchListViewAdapter(Context context, JsonArray sear

我有一个列表视图,一切正常,但我没有太多的项目。。当我向下滚动项目列表很长时,它会在某个点崩溃

这是我的适配器代码:

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;
}
}
以及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)
你能帮帮我吗?快把我逼疯了

多谢各位

编辑:第72行:
String LastScanTime[]=scanfo.getString(“LastScanTime”).split(“”)


这可能与本文有关:

问题在于你的Json代码,任何Json对象都是空的。你应该注意这一点,并在应用前检查空值

您应该通过检查空值来完成

if(mJsonObject!=null)
{
// Then perform you task
}
您的代码在可疑位置检查是否为空 如ScanInfoLastScanTime

  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]);

我可以知道什么是72行吗?是的,不知道哪一行是72行很难分辨。哈哈哈,谢谢。请查看我编辑的postPlease,点击日志中的这一行09-22 16:35:31.191:E/AndroidRuntime(31440):com.eventpulse.app.SearchListViewAdapter.getView(SearchListViewAdapter.java:72),告诉我们它表示哪一行。在日志中打印这一行。。获取字符串(“LastScanTime”)并发送给我们。谢谢。问题是listview及其元素会显示!!只有当我向下滚动列表很长时才会发生崩溃。。。就像这个家伙,就是那个。。。我得到的JSon被服务器损坏,最后两条记录没有
ScanInfo
元素。。这就是为什么…是的,我怀疑