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

标签 android nullpointerexception quickblox

关闭。这个问题需要debugging details .它目前不接受答案。












编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将帮助其他人回答问题。


7年前关闭。







Improve this question




我在 android 上的 quickblox 聊天服务中创建类型组对话框时遇到了一些问题。我能够从 quickblox 的服务器端获取所有用户,能够构建对话框并添加我在其中获得的那些用户,但是当涉及到调用 groupChatManager.createDialog(dialog, new QBEntityCallbackImpl()。我得到一个空指针异常(exception)。我后来发现我必须在扩展应用程序的类中初始化 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();
        }
    }

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

最佳答案

你是对的,

chatService.getInstance().getGroupChatManager();

如果您没有登录 ti Chat,则为 null

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

关于android - 在 quickblox 中创建对话框时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26590564/

相关文章:

android - 获取 ImageButton 的背景作为位图

java - NullPointerException 错误,将 string[] 转换为 int[]

nullpointerexception - 在upsert上的ElasticsearchIllegalArgumentException

api - 使用 GNU Parallel 进行分页

android - 将 onClick 添加到对话框布局中没有类的按钮

android无法解析json数据android

php - 从 php 执行 adb

Java getClass().getResource ("file") 导致 NullPointerException

ios - 在 quickblox 上上传个人资料图片时 Swift 出错

QuickBlox 访问存储在公共(public) blob 中的图像的直接 URL — Android 内容模块