我试图使用解析用户对象和findInBackground从Parse数据库中检索android中的名称列表但是我得到了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;
}
}
}
错误
08-27 18:54:06.039 16446-16446 / com.example.user.mycareerchat E / AndroidRuntime:FATAL EXCEPTION:main 显示java.lang.NullPointerException 在com.example.user.mycareerchat.UserList $ 1.done(UserList.java:106) 在com.example.user.mycareerchat.UserList $ 1.done(UserList.java:79) 在com.parse.ParseTaskUtils $ 2 $ 1.run(ParseTaskUtils.java:115) 在android.os.Handler.handleCallback(Handler.java:725) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 在dalvik.system.NativeStart.main(本地方法)
答案 0 :(得分:0)
这是整个UserList
课吗?
因为在提供的代码中,您只声明并使用user
对象,而不为其分配ParseUser
的实例。
public static ParseUser user; // Only a declaration, "user" still null
因此,user.getUsername()
会导致nullpointer异常,因为
user
仍为null
ParseUser.getQuery().whereNotEqualTo("username",user.getUsername()).findInBackground(new FindCallback<ParseUser>() {
您必须为user
分配ParseUser
类型的对象(或ParseUser
的子类):
public static ParseUser user;
user = new ParseUser(..., args, ...); // Now user is not null anymore and the object's methods can be called