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オブジェクトを作成する必要があります。

例えば:

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