在quickblox中创建对话框时出现空指针异常

时间:2014-10-27 15:05:42

标签: android nullpointerexception quickblox

我在android上的quickblox聊天服务中创建类型组对话框时遇到了一些问题。我能够从quickblox的服务器端获得所有用户,能够构建对话框并添加我所拥有的那些用户但是当涉及到调用groupChatManager.createDialog时(对话框,新的QBEntityCallbackImpl()。我得到一个空指针我之后发现我必须在类中初始化chatService,扩展应用程序我做了但仍然是同样的错误。这是我的代码

enter code here
My applicationSingeltonClass
public class ApplicationSingleton extends Application
{

private QBUser currentUser;
//quickBlox
private static final String APP_ID = "key";
private static final String AUTH_KEY = "authkey";
private static final String AUTH_SECRET = "authsec";
private static ApplicationSingleton instance;
private Map<Integer, QBUser> dialogsUsers = new HashMap<Integer, QBUser>();
private QBRoomChat currentRoom;

@Override
public void onCreate()
{

    super.onCreate();
    initApplication();
    initImageLoader(getApplicationContext());
}
public static ApplicationSingleton getInstance()
{
    return instance;
}
private void initImageLoader(Context context)
{
    // This configuration tuning is custom. You can tune every option, you may tune some of them,
    // or you can create default configuration by
    //  ImageLoaderConfiguration.createDefault(this);
    // method.
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
            .threadPriority(Thread.NORM_PRIORITY - 2)
            .denyCacheImageMultipleSizesInMemory()
            .discCacheFileNameGenerator(new Md5FileNameGenerator())
            .tasksProcessingOrder(QueueProcessingType.LIFO)
            .enableLogging() // Not necessary in common
            .build();
    // Initialize ImageLoader with configuration.
    ImageLoader.getInstance().init(config);
}


public QBUser getCurrentUser()
{

    return currentUser;
}

public void setCurrentUser(QBUser currentUser)

{
    this.currentUser = currentUser;
}

public Map<Integer, QBUser> getDialogsUsers()

{
    return dialogsUsers;
}

public void setDialogsUsers(List<QBUser> setUsers)
{
    dialogsUsers.clear();

    for (QBUser user : setUsers)
    {
        dialogsUsers.put(user.getId(), user);
    }
}

public void addDialogsUsers(List<QBUser> newUsers)
{
    for (QBUser user : newUsers)
    {
        dialogsUsers.put(user.getId(), user);
    }
}

public Integer getOpponentIDForPrivateDialog(QBDialog dialog)
{
    Integer opponentID = -1;
    for(Integer userID : dialog.getOccupants())
    {
        if(userID != getCurrentUser().getId())
        {
            opponentID = userID;
            break;
        }
    }

    return opponentID;
}
private void initApplication()
{
    instance = this;
    QBChatService.setDebugEnabled(true);
    QBSettings.getInstance().fastConfigInit(APP_ID, AUTH_KEY,AUTH_SECRET);

}

}

这是我的另一课,我创建了一个对话框

enter code here

protected void onPostExecute(final Boolean success)
    {
        if(status.equals("accepted"))
        {

            pagedRequestBuilder.setPage(1);
            pagedRequestBuilder.setPerPage(10);
            final ArrayList<String> usersLogins = new ArrayList<String>();
            usersLogins.add(userID);
            usersLogins.add(herocivID);
            final ArrayList<Integer> occupantIdsList = new ArrayList<Integer>();
            occupantIdsList.add(civID);
            occupantIdsList.add(heroCivID);

            QBUsers.getUsersByFacebookId(usersLogins, pagedRequestBuilder, new QBEntityCallbackImpl<ArrayList<QBUser>>() {
                @Override
                public void onSuccess(ArrayList<QBUser> users, Bundle params)
                {

                    if (!QBChatService.isInitialized())
                    {
                        QBChatService.init(getApplicationContext());
                        chatService = QBChatService.getInstance();
                        chatService.addConnectionListener(chatConnectionListener);
                    }


                    QBDialog dialog = new QBDialog();
                    dialog.setName("chat with mostafa wo may");
                    dialog.setType(QBDialogType.GROUP);
                    dialog.setOccupantsIds(occupantIdsList);

                    QBGroupChatManager groupChatManager = chatService.getInstance().getGroupChatManager();
                    groupChatManager.createDialog(dialog, new QBEntityCallbackImpl<QBDialog>()
                    {
                        @Override
                        public void onSuccess(QBDialog dialog, Bundle args)
                        {
                            Log.i("", "dialog: " + dialog);
                        }

                        @Override
                        public void onError(List<String> errors) {
                            Toast.makeText(getBaseContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
                        }
                    });

                }

                @Override
                public void onError(List<String> errors)
                {
                    Toast.makeText(getBaseContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
                }

            });

            Toast.makeText(getBaseContext(), "you accepted the request", Toast.LENGTH_SHORT).show();
        }
        else if(status.equals("rejected"))
        {
            Toast.makeText(getBaseContext(), "you rejected the request", Toast.LENGTH_SHORT).show();
        }
    }

我需要知道我必须配置聊天服务以防止在创建对话框时出现空指针。 任何帮助将不胜感激,谢谢。

2 个答案:

答案 0 :(得分:4)

你是对的,

chatService.getInstance().getGroupChatManager();
如果您未登录ti聊天

,则

为空

所以你必须先登录聊天然后再调用这个方法

答案 1 :(得分:3)

我设法解决了这个问题。 首先,您需要检查roomChatManager,如果它等于null,您必须要求用户登录。 在登录中第二个使用社交提供者或通过邮件你必须登录到chatService(ApplicationSingleton.getInstance()。chatService.login(userResult);) ApplicationSingleton是一个java类,它在它初始化ChatService时扩展了Application。 现在你可以成功创建对话了。