开发者

problem in passing data in android Activities?

开发者 https://www.devze.com 2022-12-17 06:27 出处:网络
can any one guide me what mistake am i doing in this code??? it not seems to be working.. i have two activies

can any one guide me what mistake am i doing in this code??? it not seems to be working..

i have two activies

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Intent intent = new Intent(DataPass开发者_如何学Going.this, DataPassing2.class); 

    Bundle b = new Bundle(); 
    b.putInt("key", 1123); 

    intent.putExtras(b); 

    startActivity(intent); 

    finish(); 
} 

and in second activity i have written

public void onCreate(Bundle savedInstanceState) { 

    Bundle b = getIntent().getExtras(); 
    int value = b.getInt("key", 0); 

    Toast.makeText(this, value, Toast.LENGTH_SHORT).show(); 
} 

but the code is giving me error i dont know why.. i have added second activity to manifest file.. please guide what mistake i am doing ???

any help would be appriciated..


Can you debug the code, or perhaps include some try/catch-blocks, to try and detect where the error is happening, and what the error message is?

Other than that, try doing it this way instead:

Intent intent = new Intent(DataPassing.this, DataPassing2.class); 
intent.putExtra("key", 1123); 

startActivity(intent); 

... and still fetch the bundle in DataPassing2 as you have been before. I don't know if it'll help, because I don't know much about what your error is, but it might.


Try this one may be it work.

public void onCreate(Bundle savedInstanceState) { 

    Bundle b = getIntent().getExtras(); 
    int value = b.getInt("key"); 

    Toast.makeText(this, value, Toast.LENGTH_SHORT).show(); 
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号