使用recyclerView更新数据时程序崩溃

我在编写“添加好友”模块时,根据用户输入的用户名,从服务器获取到了相应信息,但将该数据载入recyclerView更新时程序却崩溃了。接下来是我的代码,以及错误日志。求助大家,小白一只,挣扎了一星期也没找到问题所在。

此为主活动部分:思路是获取输入框的用户名,进入Bmob数据库查询。我调试的时候看到用户是成功查询到的。
public class FriendAdd extends AppCompatActivity {
private Button searchUser;
private EditText newFriendName;
private List newFriendList=new ArrayList<>();
private RecyclerView recyclerView;
private StrangerAdapter adapter;
private LinearLayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_friend);

    searchUser = (Button) findViewById(R.id.addFriend_button_fingNewFriend);
    newFriendName = (EditText) findViewById(R.id.addFriend_editText_inputFriendName);
    recyclerView=(RecyclerView)findViewById(R.id.addFriend_recyclerView);

    adapter =new StrangerAdapter();
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

    searchUser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String strangerName=newFriendName.getText().toString();
            if(strangerName.isEmpty()){
                createDialog("用户名为空","请输入您所查找用户的用户名");
            }else{
                BmobQuery<User> query = new BmobQuery<User>();
                query.addWhereEqualTo("trueName", strangerName);
                query.findObjects(new FindListener<User>() {
                    @Override
                    public void done(List<User> object, BmobException e) {
                        if(e==null){
                            for (User gameScore : object) {
                                //获得信息
                                Friend newFriend=new Friend(gameScore.gettureName(),gameScore.getdepartment());
                                newFriendList.add(newFriend);
                            }
                            adapter.setDatas(newFriendList);
                            adapter.notifyDataSetChanged();
                        }else{
                            createDialog("查询用户失败",e.getMessage()+e.getErrorCode());
                        }
                    }
                });
            }
        }
    });

}

private void createDialog(String title, String msg) {
    AlertDialog.Builder b=new AlertDialog.Builder(this);
    b.setMessage(msg);
    b.setTitle(title);
    b.setNegativeButton("重试", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog,int which){
            dialog.dismiss();
        }
    });
    b.create().show();
}

}

以下为recyclerView的数据适配器代码:
public class StrangerAdapter extends RecyclerView.Adapter {
private List stranger=new ArrayList<>();

static class ViewHolder extends RecyclerView.ViewHolder{
    ImageView friendImage;
    TextView friendName;
    TextView friendDepartment;

    public ViewHolder(View view){
        super(view);
        friendImage=(ImageView)view.findViewById(R.id.friendSearch_image_head);
        friendName=(TextView)view.findViewById(R.id.friendSearch_textView_userName);
        friendDepartment=(TextView)view.findViewById(R.id.friendSearch_textView_department);
    }
}
public StrangerAdapter() { };
public StrangerAdapter(List<Friend> friendList){
    stranger=friendList;
}
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
    View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.friend_list_item,parent,false);
    ViewHolder holder=new ViewHolder(view);
    return holder;
}
public void onBindViewHolder(ViewHolder holder,int position){
    Friend friend=stranger.get(position);
    holder.friendImage.setImageResource(friend.getImageId());
    holder.friendName.setText(friend.getName());
    holder.friendDepartment.setText(friend.getLocation());
}
public int getItemCount(){
    return stranger.size();
}

public void setDatas(List<Friend> list) {
    stranger.clear();
    if (null != list) {
        stranger.addAll(list);
    }
}

}

以下是崩溃后输出的错误日志
04-23 15:28:24.143 16212-16212/com.example.schoolpet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.schoolpet, PID: 16212
java.lang.ExceptionInInitializerError
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1070)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context cn.bmob.v3.BmobWrapper.getApplicationContext()' on a null object reference
at cn.bmob.v3.Bmob.Code(Bmob.java:117)
at cn.bmob.v3.Bmob.getApplicationContext(Bmob.java:129)
at cn.bmob.v3.b.The.(SPUtil.java:28)
at cn.bmob.v3.BmobUser.getCurrentUser(BmobUser.java:871)
at com.example.schoolpet.PetPage.(PetPage.java:59)
at java.lang.Class.newInstance(Native Method) 
at android.app.Instrumentation.newActivity(Instrumentation.java:1070) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2349) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511) 
at android.app.ActivityThread.access$900(ActivityThread.java:165) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:150) 
at android.app.ActivityThread.main(ActivityThread.java:5621) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) 

1个回答

从你的报错来看是你的context获取失败了吧?为空了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问