我的应用程序出现错误,然后当我到达listview的末尾时崩溃了。不确定导致此问题的原因以及如何解决此问题。我使用光标填充我的listview。这是我用来填充listview的代码:
How to assing an Image to Imageview based on a query results?
错误:
11-15 12:45:22.446: E/AndroidRuntime(276): FATAL EXCEPTION: main
11-15 12:45:22.446: E/AndroidRuntime(276): java.lang.NullPointerException
11-15 12:45:22.446: E/AndroidRuntime(276): at android.content.res.AssetManager.getResourceIdentifier(Native Method)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.content.res.Resources.getIdentifier(Resources.java:1422)
11-15 12:45:22.446: E/AndroidRuntime(276): at com.example.yao.zCustomUsersAdapter.getView(zCustomUsersAdapter.java:62)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.ListView.makeAndAddView(ListView.java:1727)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.ListView.fillDown(ListView.java:652)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.ListView.fillGap(ListView.java:623)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2485)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.os.Handler.handleCallback(Handler.java:587)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-15 12:45:22.446: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 12:45:22.446: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
11-15 12:45:22.446: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-15 12:45:22.446: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-15 12:45:22.446: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
对String.valueOf方法
进行NullPointer检查如: -
package com.example.yao;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class zCustomUsersAdapter extends ArrayAdapter<YAOYVD> {
public zCustomUsersAdapter(Context context, List<YAOYVD> users) {
super(context, 0, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
// User user = getItem(position);
YAOYVD user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.zitem_user, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
ImageView tvImage = (ImageView) convertView.findViewById(R.id.ivUserIcon);
// Populate the data into the template view using the data object
tvName.setText(user.getID_YAO()!=null ? String.valueOf(user.getID_YAO()): null);
//.name);
tvHome.setText(user.getID_YAO() != null ? String.valueOf(user.getID_YAO()): null);
//.hometown);
if(user.getSPIC_YAO() != null{
tvImage.setBackgroundResource(user.getSPIC_YAO());
}
return convertView;
}
}
问题可能是您的字符串值变为null或后台资源ID错误。 如果你对值进行适当的检查,那就更好了。