我试图在ParseUser对象上实现一个搜索界面(粗略地),我遇到了问题。
以下是搜索方法:
private void QueryUser() {
FlatEditText gUname;
gUname = (FlatEditText)findViewById(R.id.search_editText);
String gUsername = gUname.getText().toString();
Log.d("singleTune", "Check for "+gUsername);
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", gUsername);
query.orderByAscending(ParseConstants.KEY_USERNAME);
query.setLimit(1000);
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> users, ParseException e) {
if(e != null){
Log.d("singleTune", "Nothing found");
}else{
mUsers = users;
String[] usernames = new String[mUsers.size()];
int i = 0;
for(ParseUser user : mUsers){
usernames[i] = user.getUsername();
i++;
}
UserListAdapter adapter = new UserListAdapter(SearchActivity.this, mUsers);
setListAdapter(adapter);
}
}
});
}
这是我的适配器:
public class UserListAdapter extends ArrayAdapter<ParseUser> {
protected Context mContext;
protected List<ParseUser> mParseUsers;
public UserListAdapter(Context context, List<ParseUser> users){
super(context, R.layout.user_item, users);
mContext = context;
mParseUsers = users;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder holder;
if (rowView == null){
rowView = LayoutInflater.from(mContext).inflate(R.layout.user_item, null);
holder = new ViewHolder();
holder.avatar = (ImageView)convertView.findViewById(R.id.user_imageView);
holder.usernameLabel = (TextView)convertView.findViewById(R.id.username_textView);
holder.followButton = (FlatButton)convertView.findViewById(R.id.item_follow_button);
}
else{
holder = (ViewHolder)convertView.getTag();
}
ParseUser user = mParseUsers.get(position);
Log.d("SingleTune", "Username from query is "+user.getUsername());
holder.avatar.setImageResource(R.drawable.ic_action_picture);
holder.usernameLabel.setText(user.getString(ParseConstants.KEY_USERNAME));
return rowView ;
}
private static class ViewHolder{
ImageView avatar;
TextView usernameLabel;
FlatButton followButton;
}
}
这是我的logcat:
04-30 08:09:04.727 1144-1144/com.pixel.singletune.app E/AndroidRuntime? FATAL EXCEPTION: main java.lang.NullPointerException
at adapters.UserListAdapter.getView(UserListAdapter.java:43)
at android.widget.AbsListView.obtainView(AbsListView.java:2267)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1156)
at android.view.View.measure(View.java:15172)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15172)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148) at android.view.View.measure(View.java:15172)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
我很难过。有什么帮助吗?
答案 0 :(得分:2)
考虑convertView
为空时的情况。 rowView
将被赋予相同的值(null),因此您将输入此if
块。
View rowView = convertView;
ViewHolder holder;
if (rowView == null){
rowView = LayoutInflater.from(mContext).inflate(R.layout.user_item, null);
holder = new ViewHolder();
holder.avatar = (ImageView)convertView.findViewById(R.id.user_imageView);
holder.usernameLabel = (TextView)convertView.findViewById(R.id.username_textView);
holder.followButton = (FlatButton)convertView.findViewById(R.id.item_follow_button);
}
您为rowView
分配了一个值,但此时convertView仍然指向null
。这可能是您遇到异常的地方。您可以在findViewById
上致电rowView
而不是解决此问题。
答案 1 :(得分:1)
首先,您在空findViewById()
上呼叫convertView
,而不是最近夸大的rowView
。
您还忘记使用setTag()
存储视图持有者对象。添加
rowView.setTag(holder);
到if (rowView == null)
区块。