解决办法:给Context赋默认值,但是不能为null
可以把当前所在的类赋值给它
package com.hezilon.imdemo;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;


import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;

public class MainActivity extends AppCompatActivity {

    Context context =  MainActivity.this;
    private Button button;

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



    }

    @Override
    protected void onStart() {
        super.onStart();

        // 初始化,融云开发者后台创建的应用的 AppKey
        String appKey = "lmxuhwagl65bd";
        RongIM.init(context, appKey);

        Button btn = (Button)this.findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                Conversation.ConversationType conversationType  = Conversation.ConversationType.PRIVATE;
                //接收方 ID
                String targetId = "接收方 ID";
                String title = "聊天小姐姐";
                RongIM.getInstance().startConversation(context , conversationType, targetId, title, null);

            }
        });
    }
}

关注我的公众号SpaceObj 领取idea系列激活码

posted on 2023-05-11 09:44  张伯灵  阅读(13)  评论(0编辑  收藏  举报