获取字符串从捆绑的android返回null [英] Get string from bundle android returns null

查看:198
本文介绍了获取字符串从捆绑的android返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个活动传递到另一个字符串,在其中我写了一个

I want to pass a string from one activity to another, in one of them I wrote

公共无效pdfView(文件f){

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. //////////////////

我把它作为

   Intent intent;
    Bundle b = new Bundle();

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

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

    intent.putExtras(b);

    startActivity(intent);

和现在的工作

推荐答案

试试这样

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

这篇关于获取字符串从捆绑的android返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆