0

I have Act_01 (where I put value) and Act_02 (where I get value) but have declared these methods in a Extras class, getting value from Act_02 returns null value:

Act_01: (Where I want to pass the value Name to Act_02)

public class Act_01 extends Activity {

Extras cc_Extras;

Button btn1;
Intent intent;
String str_Name;

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

    cc_Extras = new Extras();

    str_Name = "Buck";

    btn1 = (Button) findViewById(R.id.btn1);
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            cc_Extras.putExtras();
            startActivity(intent);  
        }
      });
}
}

Act_02: (Where I want ot receive value Name from Act_01 but the app crashes with null value)

public class Act_02 extends Activity {

Extras cc_Extras;
String str_Name;

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

    cc_Extras = new Extras();

    if(getIntent() != null && getIntent().getExtras() != null)
    {
        cc_Extras.getExtras();
    }

    Toast.makeText(getApplicationContext(), "Name: "+str_Name, Toast.LENGTH_SHORT).show();
}
}

Extras: (Where I define the methods to put and get Extras)

public class Extras extends Activity {

String str_Name;
Intent intent;

public void putExtras() {
    // TODO Auto-generated method stub

    intent.putExtra("KEY_Name", str_Name);
}

public void getExtras() {
    // TODO Auto-generated method stub

    str_Name = getIntent().getExtras().getString("KEY_Name");

}
}

EDIT: I do not want to pass and get data directly between activities, I want to use the 3rd class (Extras.java) because I have too many activities having too many values between each other and want to sort of define them globally in Extras so that all my other activities can just call one method instead of getting and putting too many values in my activities.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
user3560827
  • 632
  • 1
  • 5
  • 22
  • 1
    There's no reason to create your own Extras class... If you need a sharable class similar to it, then you can use SharedPreferences – OneCricketeer Aug 19 '18 at 16:36
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Tyler V Aug 19 '18 at 16:59
  • Based on your description of what you are trying to do, I'd suggest you look into making your `Extras` class a [singleton](https://stackoverflow.com/questions/51565587/pass-objects-from-one-activity-to-another-to-change-their-values/51565687#51565687). Or see options [here](https://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities) – Tyler V Aug 19 '18 at 22:02

3 Answers3

1

On both activities you are creating a new instances of Extras class means they dont hold the same value you can do this to transfer data from A to B

public class Act_01 extends Activity {


Button btn1;
Intent intent;
String str_Name;

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


    str_Name = "Buck";

btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        intent = new Intent(Act_01.this, Act_02.class);
        intent.putExtra("data", str_Name)
        startActivity(intent);  
      }
    });
  }
}

And receieve data like this

public class Act_02 extends Activity {

String str_Name;

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

// cc_Extras = new Extras();

if(getIntent() != null)
{
  if (getIntent().getStringExtra("data") != null) {
      Toast.makeText(Act_02.this, "Name: "+getIntent.getStringExtra("data"), Toast.LENGTH_SHORT).show();
  }
}


}
}

Also you should consider using Activity Context instead of the application context

Jaswant Singh
  • 9,900
  • 8
  • 29
  • 50
  • Your intent in Act_01 needs to specify which Activity to launch too... (`new Intent(Act_01.this, Act_02.class);`) – Tyler V Aug 19 '18 at 16:54
  • HI, I know about your method and have used it and it works however I have about 50 activities that share values between each other and don't want to end up having too many lines of code and that's why I want to use Extras class – user3560827 Aug 19 '18 at 16:57
  • 1
    For that you can use SharedPrefrences to store your data as a cache and you can access that data in any activity. – Jaswant Singh Aug 19 '18 at 17:36
1

Your app crashes not with a null value, but a null pointer reference because you created a new Activity manually

cc_Extras =  new Extras();

Then called a lifecycle method on it

cc_Extras.getExtras()

Which calls getIntent(), but the Intent was never setup by the Android framework, and cc_Extras.getExtras() wouldn't have any of the data you wanted anyway in the second Activity because it was just created there, not from the first Activity.

Briefly, you should never make a new Activity, and your Extras class does not need to be an Activity in the first place (nor does it provide much benefit).

Just use the Intent object provided by the first Activity to start the second Activity, and get extras like normal. Don't overcomplicate your code. Regarding the title of the question, Intent and Bundle are already "another class" designed by Android for you to transfer data.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • My code will end up being shorter and neater if I use Extras class because I have too many activities that are sharing too many values between each other and want to create a "global" class – user3560827 Aug 19 '18 at 17:00
0

Ok! so here are the few things I might wanna suggest you to correct.

Changes needs to be done in the code.

  1. You are not assigning anything to "intent" object , and you have passed a intent without assigning anything to it.
  2. Your instance cc_Extra isn't doing anything in the activity1. You might wanna pass the "intent" object in your constructor of class like cc_Extras= new Extras(intent); and in the Extras class do the following- Intent intent; Extras(Intent i) { this.intent=i; }

  3. In the activity2 you are creating the new Instance of Extras(). So according to your code it is going to be NULL by default. If you have done the changes from the previous step, you can create new instance by doing cc_Extras(getIntent());

Corrections in the code

1) In Extras class getExtras() method instead of str=getIntent() use str=intent.getExtras.getString().

2) In the activity2 you are not assigning anything to your String str_Name, so you need to return the string you got in getExtras() method. You can do it by changing the return type to String. Below is the sample code.

public String getExtras() 
{
str_Name=intent.getExtras().getString("KEY_Name");
//OR
//str_Name=intent.getStringExtra("KEY_Name");
return str_Name;
}

3) By the doing this you need to catch this string in the activity2 by doing `

if(getIntent() != null && getIntent().getExtras() != null)
{
    str_Name=cc_Extras.getExtras();
}`

4) Another thing is you must create intent like this-

Intent intent=new Intent(currentActivityName.this,anotherActivity2.class);
//then use the intent object

EDIT- Your code must look like this in the end...

Act1

public class Act_01 extends Activity {

Extras cc_Extras=null;

Button btn1;
String str_Name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_01);
    str_Name = "Buck";
    btn1 = (Button) findViewById(R.id.btn1);
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         //changes to do
            Intent intent= new Intent(Act01.this,Act02.class);
            cc_Extras= new Extras(intent);
            cc_Extras.putExtras(str_Name);
            //end
            startActivity(intent);  
        }
      });
}
}

Act02

public class Act_02 extends Activity {

Extras cc_Extras;
String str_Name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_02);
    cc_Extras = new Extras(getIntent());

    if(getIntent() != null && getIntent().getExtras() != null)
    {
        str_Name=cc_Extras.getExtras();
    }

    Toast.makeText(getApplicationContext(), "Name: "+str_Name, Toast.LENGTH_SHORT).show();
}
}

Extras class

public class Extras { //remove "extends Activity" because it is  a class not a activity

String str_Name;
Intent intent;
Extras(Intent i)
{
    this.intent=i;
}
public void putExtras(String str) {
    // TODO Auto-generated method stub
    str_Name=str;
    intent.putExtra("KEY_Name", str_Name);
}

public String getExtras() {
    // TODO Auto-generated method stub

    str_Name = intent.getExtras().getString("KEY_Name");
    return str_Name;
}
}

Above code will work just on String. You can extend the functionality if you want.

I hope this must work to get your code working!

Mahesh Jaganiya
  • 155
  • 1
  • 2
  • 11