2012-06-06 52 views
0

我在一個將字符串傳遞給另一個片段的分片中有一個Bundle。該字符串需要在文本視圖中設置文本,並且我的方法不起作用。我不知道爲什麼,但我所有的其他字符串都經過了。Args軟件包不傳遞給Android TextView

請看看我的代碼,讓我知道我有什麼錯了 - 我不明白這一點...

來源:

public void onClick(View v) { 

     Bundle args = new Bundle(); 

     FragmentManager fm = getFragmentManager(); 
     final FragmentTransaction vcFT = fm.beginTransaction(); 
     vcFT.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out, R.anim.hyperspace_in, R.anim.slide_out); 

     switch (v.getId()) { 

      case R.id.regulatoryBtn : 

       String keyDiscriptionTitle = "Regulatory Guidance Library (RGL)"; 
       args.putString("KEY_DISCRIPTION_TITLE", keyDiscriptionTitle); 

       RegulatoryDiscription rd = new RegulatoryDiscription(); 
       vcFT.replace(R.id.viewContainer, rd).addToBackStack(null).commit(); 
       rd.setArguments(args); 
       break; 
. . . 
} 

要:

public class RegulatoryDiscription extends Fragment { 

    Bundle args = new Bundle(); 

    String DNS = "http://192.168.1.17/"; 
    String KEY_DISCRIPTION_TITLE = "KEY_DISCRIPTION_TITLE"; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.discription_view, container, false); 

     TextView title = (TextView) view.findViewById(R.id.discriptionTitle); 
     String keyDiscriptionTitle = args.getString(KEY_DISCRIPTION_TITLE); 
     title.setText(keyDiscriptionTitle); 

     return view; 
    } 
. . . 
} 

回答

4

您在RegulatoryDe​​scription Fragment中聲明args爲一個新的Bundle。這將初始化一個新的捆綁對象,它是完全空

您需要檢索您在。

前通過已經存在的論據。

public class RegulatoryDiscription extends Fragment { 
    Bundle args; 

    String DNS = "http://192.168.1.17/"; 
    String KEY_DISCRIPTION_TITLE = "KEY_DISCRIPTION_TITLE"; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.discription_view, container, false); 

     args = getArguments(); //gets the args from the call to rd.setArguments(args); in your other activity 

     TextView title = (TextView) view.findViewById(R.id.discriptionTitle); 
     String keyDiscriptionTitle = args.getString(KEY_DISCRIPTION_TITLE); 
     title.setText(keyDiscriptionTitle); 

     return view; 
    } 
} 
+0

Thnx。我只是在看它。有助於有一個新的眼睛。 – CelticParser