Get string from bundle android returns null
37,787
Solution 1
try this way
Intent intent = new Intent(first.this, second.class);
Bundle bundle = new Bundle();
bundle.putInt("index", index);
intent.putExtras(bundle);startActivity(intent);
then get it as
Bundle b = getIntent().getExtras();
int index = b.getInt("index");
Solution 2
in your other activity, instead of using
filename = getIntent().getStringExtra("filename");
try using
filename = b.getString("filename");
That should solve your problem.
Author by
Emy Alsabbagh
Updated on April 27, 2020Comments
-
Emy Alsabbagh about 4 years
I want to pass a string from one activity to another, in one of them I wrote
public void pdfView(File f) {
// f is: /data/data/com.example.iktabClasses/files/fileName.pdf Intent intent = new Intent(getApplicationContext(),NewPdfActivity.class); intent.putExtra("filename", f); startActivity(intent); }
and in the other Activity I wrote:
Bundle b=getIntent().getExtras(); if (b != null) { filename = getIntent().getStringExtra("filename"); System.out.println("filename: "+filename); }
but filename always returns as 'null'. How to solve this? Thanks in advance. //////////////////
I made it as
Intent intent; Bundle b = new Bundle(); b.putString("filename", f.toString()); intent = new Intent(getApplicationContext(),NewPdfActivity.class); intent.putExtras(b); startActivity(intent);
and Now it work