2

我的主要活动启动登录活动。如果用户成功登录,则该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();
    }
}
4

1 回答 1

1

您正在传递 getIntent() 作为 Intent 返回到 Activity

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

例如:

  Intent returnIntent = new Intent();
  returnIntent.putExtra("SelectedBook",book);
  setResult(RESULT_OK,returnIntent);       
于 2013-01-12T16:21:17.150 回答