0

我的活动中有以下代码:

ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.findInBackground(new FindCallback<ParseUser>() {
        public void done(List<ParseUser> userList, ParseException e) {
            if (e == null) {
                Log.d("score", "Retrieved " + userList.size() + " scores");
                for (int i = 0; i < userList.size(); i++) {
                    friends.add(userList.toString());
                }
                aAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, friends);
                friendChooser.setAdapter(aAdapter);
            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    });

问题是这个数组包含所有列,当我只想获取“用户名”字段时。如何仅获取“用户名”字段?
另一个问题是,因为在 User 表中有一个名为“emailVerified”的布尔字段,而 aAdapter 是一个ArrayAdapter<String内部不能有布尔值的字段,所以应用程序崩溃了。再说一次,我怎样才能只得到“用户名”字段?

崩溃的Logcat:

java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
        at com.intap.tof.GameActivity$3.done(GameActivity.java:189)
        at com.intap.tof.GameActivity$3.done(GameActivity.java:184)
        at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5832)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Logcat 因@MarcoBatista 的回答而崩溃:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
        at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
        at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
        at com.intap.tof.GameActivity$3.done(GameActivity.java:192)
        at com.intap.tof.GameActivity$3.done(GameActivity.java:184)
        at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)

错误行:
GameActivity.java:192 - friendChooser.setAdapter(aAdapter);
GameActivity.java:184 -public void done(List<ParseUser> userList, ParseException e) {

对你来说:
aAdapter是一个ArrayAdapter<String>.
friendChooser是一个MultiAutoCompleteTextView
friends是一个List<String>
userList是一个List<ParseUser>

4

2 回答 2

1

您必须从列表中获取对象,然后从中获取用户名(文档):

ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>(){
    public void done(List<ParseUser> userList, ParseException e) {
        if (e == null) {
            Log.d("score", "Retrieved " + userList.size() + " scores");
            friends = new ArrayList<>();//<-- Initialize the String array
            for (int i = 0; i < userList.size(); i++) {
                friends.add(userList.get(i).getUsername());
            }
            aAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, friends);
            friendChooser.setAdapter(aAdapter);
        } else {
            Log.d("score", "Error: " + e.getMessage());
        }
    }
});
于 2015-08-27T18:56:22.043 回答
0

而不是这个:

 friends.add(userList.toString());

尝试这个:

 friends.add(userList.get(i).getUsername());
于 2015-08-27T18:56:10.613 回答