发送消息

时间:2016-02-01 06:59:38

标签: quickblox quickblox-android q-municate

我在/com.quickblox.q_municate/ui/friends下添加了新的类文件。我想向其他用户发送消息。我用Quickblox API编写代码,代码如下。

public class ChangeDSConfiguration extends Activity{

TextView OpenSwitchRelayFor;
TextView OpenPowerRelayFor;
TextView PowerRelayCode;
TextView SwitchRelayCode;
TextView SecretPassword;
EditText OpenSwitchRelayForValue;
EditText OpenPowerRelayForValue;
EditText PowerRelayCodeValue;
EditText SwitchRelayCodeValue;
EditText SecretPasswordValue;
Button ChangeConfiguration;

String password;
String configurationDetails;
int id;

public static void start(Context context, int id) {
    Intent intent = new Intent(context, ChangeDSConfiguration.class);
    intent.putExtra(QBServiceConsts.USER_ID, id);
    context.startActivity(intent);
}

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

    id = getIntent().getExtras().getInt(QBServiceConsts.USER_ID);
    Log.e("ChangeConfig", "id = "+id);


    OpenSwitchRelayFor = (TextView) findViewById(R.id.openSwitchRelayFor);
    OpenPowerRelayFor = (TextView) findViewById(R.id.openPowerRelayFor);
    PowerRelayCode = (TextView) findViewById(R.id.powerRelayCode);
    SwitchRelayCode = (TextView) findViewById(R.id.switchRelayCode);
    SecretPassword = (TextView) findViewById(R.id.secretPassword);
    OpenSwitchRelayForValue = (EditText) findViewById(R.id.etOpenSwitchRelayFor);
    OpenPowerRelayForValue = (EditText) findViewById(R.id.etOpenPowerRelayFor);
    PowerRelayCodeValue = (EditText) findViewById(R.id.etPowerRelayCode);
    SwitchRelayCodeValue = (EditText) findViewById(R.id.etSwitchRelayCode);
    SecretPasswordValue = (EditText) findViewById(R.id.etSecretPassword);
    ChangeConfiguration = (Button) findViewById(R.id.changeConfiguration);

    ChangeConfiguration.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showAskForSecretPasswordDialog();
        }
    });
}

public void showAskForSecretPasswordDialog(){
    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.layout_enter_password, null);

    final EditText secret_password;
    secret_password = (EditText) view.findViewById(R.id.secretPassword);
    secret_password.setText("");
    secret_password.setFocusable(true);
    AlertDialog.Builder askForPasswordDialog = new AlertDialog.Builder(ChangeDSConfiguration.this);

    askForPasswordDialog.setView(view);
    askForPasswordDialog.setCancelable(false);
    askForPasswordDialog.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            password = secret_password.getText().toString();
            Log.e("FriendsListFragment", "password = " + password);
            if(password.equals("")){
                Toast.makeText(ChangeDSConfiguration.this, "Please enter password before you continue", Toast.LENGTH_LONG).show();
                return;
            }else{
                password = "Change Configuration:" + password;      // frame relay code message, with header and relay type
                sendConfigurartionDetails(password);
                finish();
            }
        }
    });

    askForPasswordDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            return ;
        }
    });

    askForPasswordDialog.setTitle(R.string.title_enter_relay_code);
    askForPasswordDialog.setMessage(R.string.message_enter_relay_code);
    askForPasswordDialog.show();

    return;
}

public void sendConfigurartionDetails(String password){

    if (!QBChatService.isInitialized()) {
        Log.e("ChangeConfig", "(!QBChatService.isInitialized())");
        QBChatService.init(this);
    }

    configurationDetails = password + ":" + getConfigurationDetails();
    Log.e("ChangeDSConfig", "configurationDetails = "+configurationDetails);

    try {
    QBChatMessage message = new QBChatMessage();
    message.setBody(configurationDetails);

    QBPrivateChatManager privateChatManager = QBChatService.getInstance().getPrivateChatManager();
        Log.e(" ChangeDSConfig", "id = "+id);
    QBPrivateChat privateChat = privateChatManager.getChat(id);
        Log.e(" ChangeDSConfig", "getChat(id);");
    if (privateChat == null) {
        privateChat = privateChatManager.createChat(id, null);
        Log.e(" ChangeDSConfig", "Private chat created");
    }

        privateChat.sendMessage(message);
        Log.e(" ChangeDSConfig", "Power Relay Code sent");
    } catch (XMPPException e) {
        Toast.makeText(ChangeDSConfiguration.this,e.getMessage(), Toast.LENGTH_LONG).show();
    } catch (SmackException.NotConnectedException e) {
        Toast.makeText(ChangeDSConfiguration.this,e.getMessage(), Toast.LENGTH_LONG).show();
    }

}

public String getConfigurationDetails(){

    String str1 = OpenSwitchRelayForValue.getText().toString();
    String str2 = OpenPowerRelayForValue.getText().toString();
    String str3 = PowerRelayCodeValue.getText().toString();
    String str4 = SwitchRelayCodeValue.getText().toString();
    String str5 = SecretPasswordValue.getText().toString();

    String str6 = str1 + ":" + str2 + ":" + str3 + ":" + str4 + ":" + str5 ;

    return str6;
}
}

我在这里得到Null点异常

    QBPrivateChat privateChat = privateChatManager.getChat(id);

1 个答案:

答案 0 :(得分:0)

显然前一个电话会返回null

QBChatService.getInstance().getPrivateChatManager();

检查您的getPrivateChatManager()实施。