无法解析方法'putExtra(java.lang.String,com.example.tuckbox.Entities.User)

时间:2019-10-21 05:31:14

标签: java android-studio android-intent methods android-room

我正在尝试使用putExtra()向新的意图中添加额外的数据,但是,在解决我尝试放置的方法时遇到了错误。Cannot resolve method 'putExtra(java.lang.String, com.example.tuckbox.Entities.User)

我尝试将Serializable和Parcelable添加到方法中,但无济于事。

我的validatePassword方法:

static final String USER_OBJECT = "USER_OBJECT";

private void validatePassword(User user) {
    if (PasswordInput.getText().toString().equals(user.getPassword())) {
      Intent intent = new Intent(this, HomeActivity.class);
      intent.putExtra(USER_OBJECT, user);
      startActivity(intent);
    }
    else{
      Toast.makeText(this, "Incorrect email or password",
              Toast.LENGTH_SHORT).show();
    }
  }

我的User班:

@Entity(tableName = "User")
public class User {
  @PrimaryKey(autoGenerate = true)
  public int UserId;

  public String Email;
  public String FirstName;
  public String LastName;
  public String Password;
  public String Phone;
  public int Address;


(设置者和获取者在那里,但是没有相关性)

1 个答案:

答案 0 :(得分:0)

错误,因为我没有在User类中获取对象。

public void openHomeActivity(User user) {
   Intent homeActivity = new Intent(this, Home.class);
   homeActivity.putExtra(USER_OBJECT, user.getUserId());
   startActivity(homeActivity);
   finish();
}