我正在研究android应用程序,我将数据从Activity 2传递回Activity 1.我通过intent传递它。问题是在Activity 1中,它在bundle中显示值,但是当我使用bundle.getString("");
时,它显示的是null值。在调试中,我正在使用bundle Bundle[{list_size=3}]
。我的代码如下,请在这里帮助我。
活动1:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==105){
TextView txtCounter = (TextView) findViewById(R.id.txtCounter);
// It showing value at MBuddle, but at MBuddle it is showing null.
Bundle MBuddle = data.getExtras();
String MMessage = MBuddle .getString("list_size");
txtCounter.setText(MMessage);
}
}
活动2:
@Override
public void onBackPressed() {
int REQUEST_CONTACTS = 105;
Intent intent = new Intent();
intent.putExtra("list_size", fListContacts.size());
setResult(REQUEST_CONTACTS, intent);
finish();
}
答案 0 :(得分:1)
在这里你必须使用getIntent()..改变你的代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==105){
TextView txtCounter = (TextView) findViewById(R.id.txtCounter);
// It showing value at MBuddle, but at MBuddle it is showing null.
Bundle MBuddle = data.getExtras();
String MMessage = MBuddle .getString("list_size");
txtCounter.setText(MMessage);
}
}
到
@Override
public void onBackPressed() {
int REQUEST_CONTACTS = 105;
Intent intent = getIntent();
intent.putExtra("list_size", fListContacts.size());
setResult(REQUEST_CONTACTS, intent);
finish();
}
活性1
var socket = io.connect("http://127.0.0.1:8002");
console.log(socket); // This works
socket.on('connect', function(){
console.log('connected'); // Does not show meaning event not fired
});
活动2
var http = require('http');
var server = http.createServer().listen(8002);
var io = require('socket.io').listen(server);
var cookie_reader = require('cookie');
var querystring = require('querystring');
var redis = require('redis');
//Configure socket.io to store cookie set by Django
io.use(function(){
io.set('authorization', function(data, accept){
if(data.headers.cookie){
data.cookie = cookie_reader.parse(data.headers.cookie);
return accept(null, true);
}
return accept('error', false);
});
// io.set('log level', 1);
// io.set('origins', 'http://127.0.0.1:8001');
});
io.sockets.on('connection', function (socket) {
console.log("Someone connected"); // Nothing shows
// Create redis client
/*client = redis.createClient();
// Subscribe to the Redis events channel
client.subscribe('notifications.' + socket.handshake.cookie['sessionid']);
// Grab message from Redis and send to client
client.on('message', function(channel, message){
console.log('on message', message);
socket.send(message);
});
// Unsubscribe after a disconnect event
socket.on('disconnect', function () {
client.unsubscribe('notifications.' + socket.handshake.cookie['sessionid']);
});*/
});
答案 1 :(得分:1)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
String contents = intent.getStringExtra(Intents.Scan.RESULT);
}}
}
===============
Intent intent = new Intent(getIntent().getAction());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.putExtra(KEY, data);
setResult(RESULT_OK, intent);
finish();
RESULT_OK可以是int值
答案 2 :(得分:0)
您应该尝试更改您的活动1,如下所示。可能是因为fListContacts.size()
返回int
值:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==105){
TextView txtCounter = (TextView) findViewById(R.id.txtCounter);
Bundle MBuddle = data.getExtras();
String MMessage = String.valueOf(MBuddle.getInt("list_size"));
txtCounter.setText(MMessage);
}
}
和活动2
将new Intent()
更改为getIntent()