第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將字符串從活動發(fā)送到片段

將字符串從活動發(fā)送到片段

喵喵時光機 2022-09-28 09:57:12
我嘗試將字符串從活動傳遞到片段。如您所見,我正在使用if語句來防止應(yīng)用程序崩潰。Toast 消息始終顯示“捆綁空”。如何防止聯(lián)邦為空?活動:public class SettingsActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    getSupportFragmentManager().beginTransaction()            .replace(android.R.id.content, new SetttingsFragment())            .commit();    Bundle bundle = new Bundle();    bundleSettings.putString("my_bundle_key", "Bundle");         SetttingsFragment setttingsFragment = new SetttingsFragment();    setttingsFragment.setArguments(bundle);片段:public class SetttingsFragment extends PreferenceFragmentCompat  {@Overridepublic void onCreatePreferences(Bundle bundle, String rootKey) {    setPreferencesFromResource(R.xml.preferences, rootKey);    Bundle bundle = getArguments();    if(bundle != null){        String bundleString = bundle.getString("my_bundle_key");        Log.i("my_bundle_key", bundleString);               } else{        Toast.makeText(getActivity(), "Bundle null", Toast.LENGTH_LONG).show();    }
查看完整描述

5 回答

?
呼啦一陣風

TA貢獻1802條經(jīng)驗 獲得超6個贊

在代碼中,您將捆綁包設(shè)置為未使用的片段變量

  1. 創(chuàng)建捆綁包

  2. 創(chuàng)建片段

  3. 在片段中設(shè)置捆綁包

  4. 顯示片段

這是您需要以片段形式傳遞參數(shù)的方式

public class SettingsActivity extends AppCompatActivity {


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    SettingsFragment settingsFragment = new SettingsFragment();

    if (savedInstanceState == null) {

        Bundle bundle = new Bundle();

        bundle.putString("my_bundle_key", "Bundle");     

        settingsFragment.setArguments(bundle);

    }

    else {

        settingsFragment.setArguments(savedInstanceState);

    }

    getSupportFragmentManager().beginTransaction()

        .replace(android.R.id.content, settingsFragment)

        .commit();


查看完整回答
反對 回復(fù) 2022-09-28
?
收到一只叮咚

TA貢獻1821條經(jīng)驗 獲得超5個贊

在調(diào)用之前,您必須調(diào)用捆綁包,如下所示:fragment

Bundle bundle = new Bundle();
        bundleSettings.putString("my_bundle_key", "Bundle");     
        SetttingsFragment setttingsFragment = new SetttingsFragment();
        setttingsFragment.setArguments(bundle);

    getSupportFragmentManager().beginTransaction()
                .replace(android.R.id.content, setttingsFragment)
                .commit();


查看完整回答
反對 回復(fù) 2022-09-28
?
Cats萌萌

TA貢獻1805條經(jīng)驗 獲得超9個贊

在您的活動中,您正在創(chuàng)建兩個片段對象,您正在執(zhí)行的第二個對象未附加到視圖且未在使用中。您的片段應(yīng)按如下方式附加到視圖:setargument

Bundle bundle = new Bundle();
bundleSettings.putString("my_bundle_key", "Bundle");     
SetttingsFragment setttingsFragment = new SetttingsFragment();
setttingsFragment.setArguments(bundle);

  getSupportFragmentManager().beginTransaction()
        .replace(android.R.id.content, setttingsFragment)
        .commit();


查看完整回答
反對 回復(fù) 2022-09-28
?
一只萌萌小番薯

TA貢獻1795條經(jīng)驗 獲得超7個贊

From Activity you send data with intent as:


Bundle bundle = new Bundle();

bundle.putString("edttext", "From Activity");

Fragmentclass obj = new Fragmentclass();

obj .setArguments(bundle);

在片段創(chuàng)建視圖方法中:


@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

    Bundle savedInstanceState) {

 String strtext = getArguments().getString("edttext");    

 return inflater.inflate(R.layout.fragment, container, false);

}


查看完整回答
反對 回復(fù) 2022-09-28
?
慕雪6442864

TA貢獻1812條經(jīng)驗 獲得超5個贊

活動


AbcFragment fragment = AbcFragment .newInstance(**Value**);

               getSupportFragmentManager.beginTransaction()

                        .replace(R.id.layout_container, fragment)

                        .commit();

在片段中


public static AbcFragment newInstance(String Value) {

        Bundle args = new Bundle();


        args.putString("Value", Value);


        AbcFragment fragment = new AbcFragment ();

        fragment.setArguments(args);

        return fragment;

    }


查看完整回答
反對 回復(fù) 2022-09-28
  • 5 回答
  • 0 關(guān)注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號