简体   繁体   中英

Retrieve a list of name in android from Parse database

I'm trying to retrieve a list of name in android from Parse database using parse user object and findInBackground but I am getting FATAL EXCEPTION: main java.lang.NullPointerException

package com.example.user.chat;

import android.app.ActionBar;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.user.mycareerchat.custom.CustomActivity;
import com.example.user.mycareerchat.utils.Utils;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by user on 26/Aug/2015.
 */
public class UserList extends CustomActivity{

    private static final String EXTRA_DATA = null ;
    private ArrayList<ParseUser> uList;

    public static ParseUser user;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_list);




        /*getActionBar().setDisplayHomeAsUpEnabled(false);*/

       updateUserStatus(true);


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        updateUserStatus(false);
    }


    @Override
    protected void onResume() {
        super.onResume();
       loadUserList();
    }

    private void updateUserStatus(boolean online) {

        user.put("online", online);
        user.saveEventually();

    }

    private void loadUserList(){

        final ProgressDialog dia = ProgressDialog.show(this, null, getString(R.string.alert_loading));

        ParseUser.getQuery().whereNotEqualTo("username",user.getUsername()).findInBackground(new FindCallback<ParseUser>() {

            @Override
            public void done(List<ParseUser> li, ParseException e) {
                dia.dismiss();
                if (li != null) {

                    if (li.size() == 0) {
                        Toast.makeText(UserList.this, getString(R.string.msg_no_user_found), Toast.LENGTH_SHORT).show();

                        ListView list = (ListView) findViewById(R.id.list);
                        uList = new ArrayList<ParseUser>(li);

                        list.setAdapter(new UserAdapter());
                        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {

                                startActivity(new Intent(UserList.this, Chat.class).putExtra(EXTRA_DATA, uList.get(pos).getUsername()));


                            }
                        });
                    }

                    else {

                        Utils.showDialog(UserList.this, getString(R.string.err_users) + " " + e.getMessage());
                    }


                }
            }
        });
    }

    private class UserAdapter extends BaseAdapter {
        @Override
        public int getCount() {

            return uList.size();
        }

        @Override
        public ParseUser getItem(int arg0) {
            return uList.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public View getView(int pos, View v, ViewGroup arg2) {

            if(v!=null)
                v=getLayoutInflater().inflate(R.layout.chat_item,null);

            ParseUser c= getItem(pos);
            TextView lbl = (TextView) v;
            lbl.setText(c.getUsername());
            lbl.setCompoundDrawablesWithIntrinsicBounds(c.getBoolean("online")? R.drawable.ic_online:
                    R.drawable.ic_offline,
                    0,R.drawable.arrow,0);
            return v;
        }
    }
}

Error

08-27 18:54:06.039 16446-16446/com.example.user.mycareerchat E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at com.example.user.mycareerchat.UserList$1.done(UserList.java:106) at com.example.user.mycareerchat.UserList$1.done(UserList.java:79) at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:115) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) 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:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method)

Is this the whole UserList class? Because in the provided code you only declare and use the user object, without assigning it an instance of ParseUser .

public static ParseUser user; // Only a declaration, "user" still null

Hence user.getUsername() results in a nullpointer exception because user is still null at the time of

ParseUser.getQuery().whereNotEqualTo("username",user.getUsername()).findInBackground(new FindCallback<ParseUser>() {

You have to assign user an object of type ParseUser (or a subclass of ParseUser ) :

public static ParseUser user;
user = new ParseUser(..., args, ...); // Now user is not null anymore and the object's methods can be called

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM