亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么我傳過去的值顯示為null?

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ?xmlns:tools="http://schemas.android.com/tools"
? ?android:layout_width="match_parent"
? ?android:layout_height="match_parent"
? ?android:orientation="vertical"
? ?tools:context="io.github.joahyau.studying.DynamicFragmentActivity">

? ?<Button
? ? ? ?android:id="@+id/load"
? ? ? ?android:layout_width="match_parent"
? ? ? ?android:layout_height="80dp"
? ? ? ?android:text="run" />

? ?<LinearLayout
? ? ? ?android:id="@+id/container"
? ? ? ?android:layout_width="wrap_content"
? ? ? ?android:layout_height="wrap_content"
? ? ? ?android:orientation="horizontal" />
</LinearLayout>


fragment xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ?android:layout_width="match_parent"
? ?android:layout_height="match_parent"
? ?android:orientation="vertical">

? ?<TextView
? ? ? ?android:id="@+id/fragment_text"
? ? ? ?android:layout_width="wrap_content"
? ? ? ?android:layout_height="wrap_content" />

</LinearLayout>


Activity的java代碼:

public class DynamicFragmentActivity extends Activity {

? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ? ? ?setContentView(R.layout.activity_dynamic_fragment);

? ? ? ?findViewById(R.id.load).setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onClick(View v) {

? ? ? ? ? ? ? ?MyFragment myFragment = new MyFragment();
? ? ? ? ? ? ? ?Bundle bundle = new Bundle();
? ? ? ? ? ? ? ?bundle.putString("text", "demo");
? ? ? ? ? ? ? ?myFragment.setArguments(bundle);

? ? ? ? ? ? ? ?FragmentManager fragmentManager = getFragmentManager();
? ? ? ? ? ? ? ?FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
? ? ? ? ? ? ? ?fragmentTransaction.add(R.id.container, myFragment);
? ? ? ? ? ? ? ?fragmentTransaction.commit();

? ? ? ? ? ?}
? ? ? ?});
? ?}
}


fragment的java代碼:

public class MyFragment extends Fragment {

? ?@Override
? ?public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

? ? ? ?View view = inflater.inflate(R.layout.myfragment, container, false);
? ? ? ?TextView textView = (TextView) view.findViewById(R.id.fragment_text);

? ? ? ?String text = getArguments().getBundle("text")+"";
? ? ? ?textView.setText(text);

? ? ? ?return view;
? ?}
}

正在回答

3 回答

怎麼解決的老哥????我也是啊TT

0 回復 有任何疑惑可以回復我~
#1

jyau 提問者

我是fragment那里用錯方法了,getString("text"),不是getBundle,你看看你的是不是這里出錯?
2018-03-21 回復 有任何疑惑可以回復我~

問題解決啦,我哭。。。

0 回復 有任何疑惑可以回復我~

對照著老師的源碼一行行看過,基本就變量名不一樣而已了。。。

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Android攻城獅的第二門課(第1季)
  • 參與學習       111145    人
  • 解答問題       1560    個

本課程由淺入深地帶您學會Android的常用控件的開發和使用

進入課程

為什么我傳過去的值顯示為null?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號