我有一个listView,一切都运行正常,我没有太多项目..当项目列表很长时,我向下滚动它在某一点崩溃...
这是我的适配器代码:
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[] = ScanInfo.getString("LastScanTime").split(" ");
这可能与此帖有关:Android ListView throwing NullPointerException when I try to scroll
答案 0 :(得分:0)
问题在于Json代码中的任何一个json对象都为null你应该注意这个并在应用之前检查null的值
您应该通过检查null valuse
来完成if(mJsonObject!=null)
{
// Then perform you task
}
您的代码在可疑位置检查是否为null 例如 ScanInfo 或 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]);