我有onActiviResult功能的问题。代码编译和模拟,但data参数返回null。
在功能主要:
private static String sActualLogin = "admin";
private static String sActualPassword = "qwerty";
(...)
public void setRegisterOnClick(View v)
{
Intent intentLOGIN = new Intent(this, Users.class);
intentLOGIN.putExtra("Type",1);
startActivityForResult(intentLOGIN, 1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data.getExtras().containsKey("Login")){
if(resultCode==RESULT_OK && requestCode==1){
String sContainer = data.getStringExtra("Login");
sActualLogin=sContainer;
RESULT1.setText(sActualLogin);
}
}
if(data.getExtras().containsKey("Password")){
if(resultCode==RESULT_OK && requestCode==1){
String sContainer=data.getStringExtra("Password");
sActualPassword=sContainer;
RESULT2.setText(sActualPassword);
}
}
}
然后代码转到用户:
package com.example.login;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Users extends Activity {
private EditText etNewLogin;
private EditText etNewPassword;
private TextView RESULT3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.users);
etNewLogin = (EditText) findViewById(R.id.etChangeLogin);
etNewLogin = (EditText) findViewById(R.id.etChangePassword);
RESULT3 = (TextView) findViewById(R.id.RESULT3);
Intent Intent2=getIntent();
int number = Intent2.getIntExtra("Type", 0);
RESULT3.setText("" +number); //returns 1 well
}
public void giveResponse(View v)
{
Intent intent = getIntent();
int iOperation = intent.getIntExtra("Type", 0);
if (iOperation == 1){
String sNewLogin = etNewLogin.getText().toString();
intent.putExtra("Login", sNewLogin);
setResult(RESULT_OK, intent);
finish();
}
if (iOperation == 2){
String sNewPassword = etNewPassword.getText().toString();
intent.putExtra("Password", sNewPassword);
setResult(RESULT_OK, intent);
finish();
}
}
}
所以我在onActivityResult之后将sActualLogin设置为“”。请帮我解决这个问题;那里出了点问题,我看不到它。
答案 0 :(得分:2)
假设您使用Users
开始startActivityForResult(intent, 1);
活动,我唯一看到的是您为两个EditText
视图设置了相同的变量:
etNewLogin = (EditText) findViewById(R.id.etChangeLogin);
etNewLogin = (EditText) findViewById(R.id.etChangePassword);
您还可以在if
上添加onActivityResult(...)
语句,以确保其不为空:
if(data.getExtras().containsKey("Login") && !data.getStringExtra("Login").isEmpty())