Home > Software design >  Getting null in return when calling model class getter methods in second activity
Getting null in return when calling model class getter methods in second activity

Time:06-13

First I initialized a model class all attributes value in first activity by calling its constructor like this UserName userName=new UserName("UnknownPerson","7dfgdufdbid_bjd"); but when I'm calling the getter methods of this model class in second activity it returning null.How to get that values in second activity without intent.putExtra().Look at my Code:


public class UserName {
    String name,userId;
    public UserName(String name, String userId) {
        this.name = name;
        this.userId = userId;
    }
    public UserName() {
    }
    public String getName() {
        return name;
    }
    public String getUserId() {
        return userId;
    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UserName userName=new UserName("UnknownPerson","7dfgdufdbid_bjd");
        Log.d("Test",userName.getName() " " userName.getUserId());
        findViewById(R.id.button).setOnClickListener(v -> {
            startActivity(new Intent(this,MainActivity2.class));
        });
    }
}
public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        UserName userName=new UserName();
        Log.d("Test",userName.getName() " " userName.getUserId());
    }
}

CodePudding user response:

It is correct to return null, if you want to get the object of the first page on the second page, you should serialize UserName first. Then call a function like the following on the first page:

Intent i = new Intent(MainActivity.this, MainActivity2.class);
i.putExtra(key, value);
startActivity(i)

On the second page, use getIntent().getExtras().getParcelable(key) to get the serialized object passed from the first page,In Android we generally use Parcelable for serialization

CodePudding user response:

you are creating new object in MainActivity2 that's why it is returning null. you should pass data in bundle.

 public Bundle toBundle(UserName username) {
            Bundle b = new Bundle();
            b.putString("userid",username.id);
            b.putString("username", username.name);
          
            return b;
        }

public class MainActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    UserName userName=new UserName("UnknownPerson","abc");
    Log.d("Test",userName.getName() " " userName.getUserId());
    Intent intent = Intent(this, MainActivity2.class);
    intent.putExtras(toBundle(username))
    findViewById(R.id.button).setOnClickListener(v -> {
        startActivity(intent);
    });
}

}

 public class MainActivity2{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            Bundle b = this.getIntent().getExtras();
            Log.d("Test",userName.getName() " " b.getString("username"));
            Log.d("Test",userName.getId() " " b.getString("userid"));
        }
    }

hope it works!!

  • Related