gpt4 book ai didi

android - 回收站 View : No adapter attached; skipping layout with android socket io

转载 作者:行者123 更新时间:2023-11-30 04:54:17 37 4
gpt4 key购买 nike

我正在尝试使用 node js 服务器和 socket.io 进行 android 实时聊天这是聊天框 Activity :

public class ChatBoxActivity extends AppCompatActivity {
public RecyclerView myRecylerView ;
public List<Message> MessageList ;
public ChatBoxAdapter chatBoxAdapter;
public EditText messagetxt ;
public Button send ;
//declare socket object
private Socket socket;

public String Nickname ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_box);

messagetxt = (EditText) findViewById(R.id.message) ;
send = (Button)findViewById(R.id.send);
// get the nickame of the user
Nickname= (String)getIntent().getExtras().getString(MainActivity.NICKNAME);
//connect you socket client to the server
try {
socket = IO.socket("http://10.0.2.2:3000");
socket.connect();
socket.emit("join", Nickname);
} catch (URISyntaxException e) {
e.printStackTrace();

}
//setting up recyler
MessageList = new ArrayList<>();
myRecylerView = (RecyclerView) findViewById(R.id.messagelist);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
myRecylerView.setLayoutManager(mLayoutManager);
myRecylerView.setItemAnimator(new DefaultItemAnimator());



// message send action
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//retrieve the nickname and the message content and fire the event messagedetection
if(!messagetxt.getText().toString().isEmpty()){
socket.emit("messagedetection",Nickname,"jfjdjfj");

messagetxt.setText(" ");
}


}
});

//implementing socket listeners
socket.on("userjoinedthechat", new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String data = (String) args[0];

Toast.makeText(ChatBoxActivity.this,data, Toast.LENGTH_SHORT).show();

}
});
}
});
socket.on("userdisconnect", new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String data = (String) args[0];

Toast.makeText(ChatBoxActivity.this,data, Toast.LENGTH_SHORT).show();

}
});
}
});
socket.on("message", new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
try {
//extract data from fired event

String nickname = data.getString("senderNickname");
String message = data.getString("message");

// make instance of message

Message m = new Message(nickname,message);


//add the message to the messageList

MessageList.add(m);

// add the new updated list to the dapter
chatBoxAdapter = new ChatBoxAdapter(MessageList);

// notify the adapter to update the recycler view

chatBoxAdapter.notifyDataSetChanged();

//set the adapter for the recycler view

myRecylerView.setAdapter(chatBoxAdapter);

} catch (JSONException e) {
e.printStackTrace();
}


}
});
}
});
}

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

socket.disconnect();
}
}

这是 Node js 服务器代码:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.send('<h1>Hello world</h1>');
});

io.on('connection',function(socket){
console.log('one user connected '+socket.id);




socket.on('CHAT' , function (data) {
console.log('======CHAT message========== ');
console.log(data);
socket.emit('CHAT',data);

});

socket.on('disconnect',function(){
console.log('one user disconnected '+socket.id);
});

})

http.listen(3000,function(){
console.log('server listening on port 3000');
})

有关错误的详细信息:服务器运行正常,它在控制台中打印“服务器监听端口 3000”。当我加入聊天时,套接字“加入”和“断开连接”也会发出,因为我在我的控制台日志中得到了这个:

一个用户连接了 xVs1-xYtvNA5sd-dAAAA

一个用户断开连接 xVs1-xYtvNA5sd-dAAAA

仅发出连接和断开套接字事件,但不发出发送和接收消息。

最佳答案

你只需要在给它layoutManager的同时给recyclerView一个adapter...它在获取adaper时跳过了这里的布局

[更新]对于没有在您的服务器上发出消息的套接字,您没有在收听“消息”,或者在 Android 端您没有收听“聊天”,我会修改您的服务器端代码以匹配您的 Android 应用程序

最终代码:

public class ChatBoxActivity extends AppCompatActivity {
public RecyclerView myRecylerView ;
public List<Message> MessageList ;
public ChatBoxAdapter chatBoxAdapter;
public EditText messagetxt ;
public Button send ;
//declare socket object
private Socket socket;

public String Nickname ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_box);

messagetxt = (EditText) findViewById(R.id.message) ;
send = (Button)findViewById(R.id.send);
// get the nickame of the user
Nickname= (String)getIntent().getExtras().getString(MainActivity.NICKNAME);
//connect you socket client to the server
try {
socket = IO.socket("http://10.0.2.2:3000");
socket.connect();
socket.emit("join", Nickname);
} catch (URISyntaxException e) {
e.printStackTrace();

}
//setting up recyler
MessageList = new ArrayList<>();
myRecylerView = (RecyclerView) findViewById(R.id.messagelist);




// message send action
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//retrieve the nickname and the message content and fire the event messagedetection
if(!messagetxt.getText().toString().isEmpty()){
socket.emit("message",Nickname,"jfjdjfj");

messagetxt.setText(" ");
}


}
});

//implementing socket listeners
socket.on("userjoinedthechat", new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String data = (String) args[0];

Toast.makeText(ChatBoxActivity.this,data, Toast.LENGTH_SHORT).show();

}
});
}
});

socket.on("userdisconnect", new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String data = (String) args[0];

Toast.makeText(ChatBoxActivity.this,data, Toast.LENGTH_SHORT).show();

}
});
}
});

socket.on("message", new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
try {
//extract data from fired event

String nickname = data.getString("senderNickname");
String message = data.getString("message");

// make instance of message

Message m = new Message(nickname,message);


//add the message to the messageList

MessageList.add(m);

// add the new updated list to the dapter
chatBoxAdapter = new ChatBoxAdapter(MessageList);

// notify the adapter to update the recycler view

chatBoxAdapter.notifyDataSetChanged();

//set the adapter for the recycler view
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
myRecylerView.setLayoutManager(mLayoutManager);
myRecylerView.setItemAnimator(new DefaultItemAnimator());
myRecylerView.setAdapter(chatBoxAdapter);

} catch (JSONException e) {
e.printStackTrace();
}


}
});
}
});
}

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

socket.disconnect();
}
}

服务器

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.send('<h1>Hello world</h1>');
});

io.on('connection',function(socket){
console.log('one user connected '+socket.id);




socket.on('message' , function (data) {
console.log('======CHAT message========== ');
console.log(data);
socket.emit('message',data);

});

socket.on('disconnect',function(){
console.log('one user disconnected '+socket.id);
});

})

http.listen(3000,function(){
console.log('server listening on port 3000');
})

关于android - 回收站 View : No adapter attached; skipping layout with android socket io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59501120/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com