Gsurha
Gsurha

Reputation: 43

Android Set/Get Get() returns null in Android

I've 3 activities. First activity sets id, activity second is menu and activity third to show id.

But, third activity shows null id. Why?

a.class:

package a.b.c.model;
public class Profil {   
    private String pid;
    public String getpId() {
        return pid;
    }
    public void setpId(String id) {
        this.pid = pid;
    }
}

b.class:

package a.b.c;
import a.b.c.model;
public class bextends Activity {
    Profil p = new Profil();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b);
        p.setpId("TEST");
    }
}  

c.class:

package a.b.c;
import a.b.c.model;
public class c extends Activity {
    Profil p = new Profil();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c);
        final TextView tex = (TextView) findViewById(R.id.pid);
        tex.setText(p.getpId());
    }
}

d.class

package a.b.c.model;
public class d extends Activity {   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.d);;
    }
}

UPDATE: activity class b -> d -> c

Upvotes: 0

Views: 1061

Answers (3)

King of Masses
King of Masses

Reputation: 18775

In your b.class your creating a new object and setting the value to the object means it is a local object and you can use with in the class reference only. In your c.class when ever your crating a new object means it is freshly created i.e it don't have any values in it.

When ever you created an object like below means, it always empty and it having only null values in it.

       Profil p = new Profil(); 

So coming to your solution, you can achieve it by many ways based on your specifications / requirements /scope

one way is make your classes like below

a.class

package a.b.c.model;
public class Profil implements Serializable {   

        /**  auto generated serial version id
     * 
     */
    private static final long serialVersionUID = 1L;

    private String pid;
    public String getpId() {
        return id;
    }
    public void setpId(String id) {
        this.id = id;
    }
}

b.class

package a.b.c;
import a.b.c.model;
public class bextends Activity {
    Profil p = new Profil();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b);
        p.setpId("TEST");
}

after some button action click (navigating from b to c) you can send your Serialized Profile object to B class to C and then you can use it

your intent should be like this

Intent i = new Intent(this, D.class);
i.putExtra("sampleObject", p);
startActivity(i);

In your 'D' activity you need to get the object which your sending form activity b.

d.class

package a.b.c.model;
public class d extends Activity {   

    Profil p = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.d);

         p = (Profil)getIntent().getSerializableExtra("sampleObject");
    }
}

your intent should be like this (D activity to C activity)

// this you can write in your navigation part probably in a button click
Intent i = new Intent(this, C.class);
i.putExtra("sampleObject", p);
startActivity(i);

After receiving object when ever your navigating from activity 'D' to activity 'C' you need to pass the updated object in the same way

Then your C class should be like this

C.class

package a.b.c;
import a.b.c.model;
public class c extends Activity {
    Profil p = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c);

         p = (Profil)getIntent().getSerializableExtra("sampleObject");

        final TextView tex = (TextView) findViewById(R.id.pid);
        tex.setText(p.getpId());
    }
}

That's it :) now you can use the updated Profile object in your C class as well..

Note:: As i told earlier this is a basic approach.. there is too many alternative approaches will do the same operation better than this

Upvotes: 0

MeetTitan
MeetTitan

Reputation: 3568

If you intend pid to be global to the Profil class, you should make the pid variable static. A static variable belongs to the class itself, and not the instance of said class.

So our fix would look as simple as private static String pid;, and making our getters and setters static as well.

If you wish for each Profil to be able to have its own pid, then you'll need to pass the correct instance of Profil to where it is needed. A new instance of a class is generally always created with the new keyword. So remember, if we see new there's a new instance, and any non static variables are not kept and are separate from other instances.

Upvotes: 2

mbmc
mbmc

Reputation: 5115

In c.class, p is a new object with pid == null. You need to pass it from b to c through a Bundle.

Upvotes: 1

Related Questions