gpt4 book ai didi

android - 我的自定义 ListView 适配器上的致命异常

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:05 25 4
gpt4 key购买 nike

我试图在 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;
}
}

这是我的日志:

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)

我难住了伙计们。有帮助吗?

最佳答案

考虑 convertView 为 null 的情况。 rowView 将被分配相同的值(null),因此您将进入此 if block 。

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。这可能是您遇到异常的地方。您可以在 rowView 上调用 findViewById 来解决这个问题。

关于android - 我的自定义 ListView 适配器上的致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23388542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com