onActivityResult MainActivity上的Intent为null

时间:2013-01-12 16:17:33

标签: java android

我的主要活动启动了登录活动。如果用户成功登录,ApiResponseHandler对象将调用activity.finish()。似乎一切都已正确完成。我在传递可能导致null的意图时看不到任何差距。

MainActivity

中的评论中注明了错误点
    public class MainActivity extends Activity {
        static final int LOGIN_INTENT_ID = 0;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            //launch login activity
            startActivityForResult(new Intent(this, LoginActivity.class), LOGIN_INTENT_ID);
        }

        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);

            //handle activity response
            if (requestCode == LOGIN_INTENT_ID) {
                if (resultCode == Activity.RESULT_OK) {
//intent is null, so .getSerializableExtra() fails
                    User user = (User) intent.getSerializableExtra("User");
                    Toast.makeText(getApplicationContext(), "Logged in as: " + user.getFirstName() + " " + user.getLastName(), Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

我的登录活动:

public class LoginActivity extends Activity {
    public static final Logger logger = Logger.getLogger(LoginActivity.class.getName());

    Button      loginButton;
    EditText    loginField;
    EditText    passwordField;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        loginButton = (Button)findViewById(R.id.loginFormLabelButton_Login);
        loginField = (EditText)findViewById(R.id.loginFormLogin);
        passwordField = (EditText)findViewById(R.id.loginFormPassword);
    }

    /**
     * Login a user when the button is clicked
     * @param v
     */
    public void logUserIn(View v) {
        loginButton.setText(R.string.loginFormLabelButton_Login_Working);

        ApiRequest request = new ApiRequest();

        request.setLogin(loginField.getText().toString());
        request.setPassword(passwordField.getText().toString());

        if (request.getLogin().length() == 0) {
            showErrorDialog(getString(R.string.loginErrorDialog_LoginRequired));
            return;
        }
        if (request.getPassword().length() == 0) {
            showErrorDialog(getString(R.string.loginErrorDialog_PasswordRequired));
            return;
        }

        //make request and handle results
        ApiRequestHandler<User> apiHandler = new ApiRequestHandler<User>(User.class);
        apiHandler.setUrl(getString(R.string.loginFormApiUrl));
        apiHandler.setApiRequest(request);
        apiHandler.setResponseHandler(new ApiResponseHandler(this, getIntent()));
        apiHandler.execute();

        loginButton.setText(R.string.loginFormLabelButton_Login);
    }

    ....
}

new ApiResponseHandler(this, getIntent())看起来像这样......

public class ApiResponseHandler implements com.Bible_Bowl_Management.Api.ApiResponseHandler<User> {
    private Activity activity;
    private Intent intent;

    public ApiResponseHandler(Activity activity, Intent intent) {
        this.activity = activity;
        this.intent = intent;
    }

    @Override
    public void ResponseSuccessful(User user) {
        intent.putExtra("User", user);
        activity.setResult(Activity.RESULT_OK);
        activity.finish();
    }

    @Override
    public void ResponseNoContent() {
        Toast.makeText(this.activity.getApplicationContext(), "No account found with these credentials", Toast.LENGTH_LONG).show();
    }
}

1 个答案:

答案 0 :(得分:1)

您将getIntent()作为Intent传递给Activity

您应该为此创建一个新的Intent对象。

例如:

  Intent returnIntent = new Intent();
  returnIntent.putExtra("SelectedBook",book);
  setResult(RESULT_OK,returnIntent);