onActivityResult方法不起作用。

时间:2012-10-25 00:05:11

标签: android android-activity

我有一个小应用程序,它从我的主应用程序活动中启动“登录”活动作为子活动。登录活动使用EditText字段收集用户名和密码,然后在单击提交按钮后将凭据发送到主活动以进行验证。但是出于某种原因,我的onActivityResult()无效。我甚至尝试包含一个TextView,我将在方法的不同点处操作以推断它出错的地方,但似乎该方法本身由于某种原因没有执行。任何帮助将不胜感激。

我的主要活动:

public class MainActivity extends Activity {

private final String userName = "9590778";
private final String userPass = "1234567";

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

@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void activity_about(View view){
    Intent aboutIntent = new Intent(this, AboutActivity.class);
    startActivity(aboutIntent);
}

public void activity_login(View view){
    Intent loginIntent = new Intent(this, LoginActivity.class);
    startActivityForResult(loginIntent, LoginActivity.LOGIN_REQUEST);
}

public void OnActivityResult(int requestCode, int responseCode, Intent resultIntent){
    String passedUser;
    String passedPass;

    Intent intent = getIntent();

    TextView lblTest = (TextView)findViewById(R.id.lblTest);
    lblTest.setText("ATLEAST THIS IS WORKING!"); //THIS IS WHERE I AM TRYING TO MANIPULATE THE TEXTVIEW TO SEE IF METHOD IS EXECUTING!

    if(requestCode == LoginActivity.LOGIN_REQUEST){

        passedUser = intent.getStringExtra("PASSED_USER");
        passedPass = intent.getStringExtra("PASSED_PASS");

        if(passedUser.equals(userName)){
            if(passedPass.equals(userPass)){
                lblTest.setText("LOGIN SUCCESSFUL!");
            }
        }
        else{
            lblTest.setText("ACCESS DENIED!");
        }
    }
}

}

另外,这是我的LoginActivity:

public class LoginActivity extends Activity {

public static final int LOGIN_REQUEST = 9999;
public static final int LOGIN_RESPONSE = 1234;

private String passedUser;
private String passedPass;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_login, menu);
    return true;
}

public void submitCred(View view){

    Intent intent = getIntent();

    EditText lblUser = (EditText)findViewById(R.id.txtUserName);
    EditText lblPass = (EditText)findViewById(R.id.txtPassword);

    passedUser = lblUser.getText().toString();
    passedPass = lblPass.getText().toString();


    intent.putExtra("PASSED_USER", passedUser);
    intent.putExtra("PASSED_PASS", passedPass);
    setResult(LOGIN_RESPONSE, intent);

    finish();
}
}

1 个答案:

答案 0 :(得分:7)

你有一个拼写错误,你想要的功能是onActivityResult,(注意小写o)。

在您想要覆盖的任何方法之前使用@Override语句是一个好习惯。如果你在OnActivityResult之前放置了@Override,你会抱怨OnActivityResult不是超类方法。

这是一个很好的检查,以确保您有正确的名称和正确的参数。