0

I want to send int parameter in intent like this:

String textname = (String) dataItem.get("name");

Intent m = new Intent(list.this,main.class);
m.putExtra("name",textname);
m.putExtra("page",1);

startActivity(m);

and in main class I get that parameter

Intent intent = getIntent();
name = intent.getStringExtra("name");
page1 = Integer.parseInt(intent.getStringExtra("page"));   

but when I run my code it force closes!!!

Mithun
  • 2,075
  • 3
  • 19
  • 26
zahra
  • 35
  • 1
  • 10

3 Answers3

2

You should use getIntent().getIntExtra(name, defaultValue) instead of Integer.parseInt(intent.getStringExtra("page"));

Update:

int defaultValue = -1;// take any default value of your choice

String name = intent.getStringExtra("name");
int page1 = intent.getIntExtra("page", defaultValue); 
Mithun
  • 2,075
  • 3
  • 19
  • 26
  • i sent tow parameters "name" as string and "page" as integer @Mitun – zahra May 25 '15 at 20:02
  • @zahra if it helped, please accept it as answer... or it will remain unsolved :) – Mithun May 25 '15 at 20:34
  • what do you mean? are you not able to get `int` data with above method? – Mithun May 26 '15 at 08:38
  • i get tow parameter "page " and "textname" and i send these i think one of the parameter have problem and one of this is null but i dont know how solve that! – zahra May 26 '15 at 08:50
  • that is your implementation issue right? But if with above method you are able to get proper value for `"page" ` `int` extra, it is a valid answer... However, for each `extra` of `int`, `String`, etc data types you put as extra into `Bundle` of sending `Intent` you can get with corresponding API to pull those extras. Check for methods available as mentioned in http://developer.android.com/reference/android/content/Intent.html – Mithun May 26 '15 at 08:55
  • my problem in function in main class and i solve it:) – zahra May 27 '15 at 06:55
1

I in other class use the main class and send parameter to it and it work without any problem but just in list class i have problem in other class i like this send parameter

protected void onListItemClick(ListView l, View v, int position, long id) {

    Intent i = new Intent(list_sub.this,main.class);
    i.putExtra("name", Name[position]);
    i.putExtra("page", Ted[position]);

    startActivity(i);
}

and in refresh class i get ted parameter

private void refresh(){

    db.open();
    int s = db.List_count("text", ID);
    Name= new String[s];
    Ted=new String[s];
    for(int i=0;i<s;i++){

        Name[i]=db.List_display_sub("text", ID, i);
        Ted[i]=db.page_count("text", Name[i].toString())+"";

    }
db.close();
    }
zahra
  • 35
  • 1
  • 10
0

Activity A

String textname = (String) dataItem.get("name");
        Intent m = new Intent(list.this,main.class);
        m.putExtra("name",textname);
        m.putExtra("page",1);
        startActivity(m);

Activity B

    Intent intent = getIntent();
    name = intent.getStringExtra("name");
    int page = intent.getIntExtra("page", 0);

where 0 is the default value.

vrbsm
  • 1,188
  • 15
  • 22