2013-04-08 43 views
6

我想從一個活動傳遞到另一個字符串,在其中我寫獲取字符串空

公共無效pdfView(文件f){

// f is: /data/data/com.example.iktabClasses/files/fileName.pdf 

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

intent.putExtra("filename", f); 

    startActivity(intent); 

} 

,並在一個其他活動我寫道:

Bundle b=getIntent().getExtras(); 

     if (b != null) { 

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

     System.out.println("filename: "+filename); 
    } 

但文件名總是返回爲'null'。 如何解決這個問題? 在此先感謝。 //////////////////

我把它作爲

Intent intent; 
    Bundle b = new Bundle(); 

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

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

    intent.putExtras(b); 

    startActivity(intent); 

和現在工作

回答

15

嘗試這種方式

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

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

intent.putExtras(bundle);startActivity(intent); 

然後把它作爲

Bundle b = getIntent().getExtras(); 
int index = b.getInt("index"); 
+1

謝謝你,你的代碼引導我。 – 2013-04-08 14:20:46

+1

謝謝,你的代碼也引導了我。 – teardrop 2017-01-24 14:19:07

1

在你的其他活動,而不是使用

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

使用try

filename = b.getString("filename"); 

這應該可以解決您的問題。