2014-10-02 55 views
1

我需要將我的FragmentActivity中的對象傳遞給我的片段。我有這段代碼試圖從FragmentActivity傳遞一個字符串,但是當我嘗試進入Fragment時,我得到了NullPointerException。無法將對象或字符串從FragmentActivity傳遞到片段

Bundle bundle = new Bundle(); 
    Descripcion f2= new Descripcion(); 
    bundle.putString("lugar", lugar.getDescripcionLugar()); 
    //bundle.putSerializable("lugar", lugar);  
    f2.setArguments(bundle);   
    getSupportFragmentManager().beginTransaction().replace(R.id.principal, f2).commit(); 

片段中的我......

private String des; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);   
    des=getArguments().getString("lugar"); // Here I get NullPointerException  
} 

我如何傳遞一個字符串或對象?

謝謝。

回答

0

第一個片段:

  Intent firstpage = new Intent(getActivity().getBaseContext(), 
        NavigationActivity.class); 

      // String message = "keels"; 

      if (name == "Keels Super") { 
       int id1 = 1; 
       firstpage.putExtra("message", id1); 
      } 

第二片段的活動:

Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 

     String result = extras.getString("message"); 
     int res = extras.getInt("message"); 
     System.out.println("yeah" + result); 
相關問題