从bundle获取字符串android返回null

Emy*_*agh 8 android bundle android-activity

我希望将一个字符串从一个活动传递到另一个活动,其中一个是我写的

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);

}
Run Code Online (Sandbox Code Playgroud)

在我写的其他活动中:

  Bundle b=getIntent().getExtras();

        if (b != null) {

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

       System.out.println("filename: "+filename);
    } 
Run Code Online (Sandbox Code Playgroud)

但文件名始终返回为"null".怎么解决这个?提前致谢.//////////////////

我做到了

   Intent intent;
    Bundle b = new Bundle();

    b.putString("filename", f.toString());

    intent = new Intent(getApplicationContext(),NewPdfActivity.class);

    intent.putExtras(b);

    startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

现在它起作用了

Sen*_*hil 21

试试这种方式

Intent intent = new Intent(first.this, second.class);

Bundle bundle = new Bundle();
bundle.putInt("index", index);

intent.putExtras(bundle);startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

然后得到它

Bundle b = getIntent().getExtras();
int index = b.getInt("index");
Run Code Online (Sandbox Code Playgroud)