-8

Am passing my integer variable from FirstPage to FifthPage, i can see 0 in my toast not the actual value stored in 'q1'. How can i pass my integer variable from one activity to other activity? Can any one help me....thanks

FirstPage.java

int pos =poltype.getSelectedItemPosition();
int q1=pos;
Intent myIntent = new Intent(context1, FifthPage.class);
myIntent.putExtra("Q1", (int)q1);                      

Intent myintent2 = new Intent(context1, SecondPage.class);
startActivity(myintent2);


FifthPage.java

Intent mIntent = getIntent();
int Q1 = mIntent.getIntExtra("Q1", 0);

Toast.makeText(FifthPage.this,
String.valueOf(Q1), Toast.LENGTH_LONG)
                           .show();

Log cat:

05-19 16:06:44.557: E/AndroidRuntime(2922): FATAL EXCEPTION: main
05-19 16:06:44.557: E/AndroidRuntime(2922): Process: com.example.techback, PID: 2922
05-19 16:06:44.557: E/AndroidRuntime(2922): android.content.res.Resources$NotFoundException: String resource ID #0x0
05-19 16:06:44.557: E/AndroidRuntime(2922):     at android.content.res.Resources.getText(Resources.java:274)
05-19 16:06:44.557: E/AndroidRuntime(2922):     at android.widget.Toast.makeText(Toast.java:277)
05-19 16:06:44.557: E/AndroidRuntime(2922):     at com.example.techback.FifthPage$1.onClick(FifthPage.java:50)
05-19 16:06:44.557: E/AndroidRuntime(2922):     at android.view.View.performClick(View.java:4756)
05-19 16:06:44.557: E/AndroidRuntime(2922):     at android.view.View$PerformClick.run(View.java:19749)
05-19 16:06:44.557: E/AndroidRuntime(2922):     at android.os.Handler.handleCallback(Handler.java:739)
05-19 16:06:44.557: E/AndroidRuntime(2922):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-19 16:06:44.557: E/AndroidRuntime(2922):     at android.os.Looper.loop(Looper.java:135)
05-19 16:06:44.557: E/AndroidRuntime(2922):     at android.app.ActivityThread.main(ActivityThread.java:5221)
05-19 16:06:44.557: E/AndroidRuntime(2922):     at java.lang.reflect.Method.invoke(Native Method)
05-19 16:06:44.557: E/AndroidRuntime(2922):     at java.lang.reflect.Method.invoke(Method.java:372)
05-19 16:06:44.557: E/AndroidRuntime(2922):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
05-19 16:06:44.557: E/AndroidRuntime(2922):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Santhosh S
  • 99
  • 2
  • 7
  • if i remove 0, i get this error "The method getIntExtra(String, int) in the type Intent is not applicable for the arguments (String)" – Santhosh S May 19 '15 at 09:37

4 Answers4

1

Use this As you have to send values from different intents not from the single one

int pos =poltype.getSelectedItemPosition();
int q1=pos;
Intent myIntent = new Intent(context1, FifthPage.class);
myIntent.putExtra("Q1", q1);                      
startActivity(myintent);

Intent myintent2 = new Intent(context1, SecondPage.class);
startActivity(myintent2);
Quick learner
  • 10,632
  • 4
  • 45
  • 55
1

In FirstPage, use:

int pos =poltype.getSelectedItemPosition();
int q1=pos;
Intent myIntent = new Intent(context1, FifthPage.class);
myIntent.putExtra("Q1", (int)q1);
startActivity(myintent);

In FifthPage Activity, use this

Intent mIntent = getIntent();
Bundle bundle = mIntent.getExtras();

    if (bundle != null) {
        // getting Q1
        int q1 = bundle.getInt("Q1");

    }

Edit: If you don't want to start Fifthpage, you can save Q1 value to SharedPreference and access it from there.

SharedPreference Usage:

In FirstPage:

SharedPreference sp = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor spe = sp.edit();
spe.putInt("Q1", (int)q1);
spe.commit();

In FifthPage:

SharedPreference sp = getPreferences(Context.MODE_PRIVATE);
int q1 = sp.getInt("Q1", 0);
Amrit Pal Singh
  • 7,116
  • 7
  • 40
  • 50
0

You need only one intent, not two and you have to start your new activitiy with the intent that have your integer (myIntent not myIntent2).

Intent intent = new Intent(CurrentClass.this, NewClass.class);
intent.putExtra("name", integerValue)
startActivity(intent);
Mann
  • 661
  • 5
  • 9
  • I need to pass the interger value to FifthPage, If i put startActivity(intent); it will start FifthPage but i dont want in my case – Santhosh S May 19 '15 at 09:42
0

Start the activity with the intent, in which is your extra added and then try with this one when you want to get your value:

int Q1 = Integer.parseInt(getIntent().getExtras().get("Q1").ToString());
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30