Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get string from bundle android returns null

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

like image 570
Emy Alsabbagh Avatar asked Apr 08 '13 13:04

Emy Alsabbagh


2 Answers

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");
like image 68
Senthil Avatar answered Oct 25 '22 03:10

Senthil


in your other activity, instead of using

filename = getIntent().getStringExtra("filename");

try using

filename = b.getString("filename");

That should solve your problem.

like image 30
dberm22 Avatar answered Oct 25 '22 03:10

dberm22