簡體   English   中英

使用bundle對象在Fragment之間傳遞數據

[英]Passing data between Fragments with bundle object

我有兩個空白的活動項,其中包含兩個片段。一個活動是具有適配器,並且它充當具有適配器內部的Tabactivity。

我想通過意圖和捆綁內傳遞數據。 當用戶在活動1中輸入電子郵件並按按鈕時,電子郵件將出現在活動2中的TextView上。

這是我得到的異常:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.presenter.utils.ui.AutoResizeTextView.setText(java.lang.CharSequence)' on a null object reference
                                                            at com.presenter.view.fragment.ForgotPasswordFragmentComplete.onCreate(ForgotPasswordFragmentComplete.java:50)
                                                            at android.support.v4.app.Fragment.performCreate(Fragment.java:2075)

我已經創建了Intent和bundle:

@Bind(R.id.btn_forgot)
    AppCompatButton btn_forgot;
    @Bind(R.id.forgot_edittext)
    AppCompatEditText editEmail;

public void onButtonClick() {
  Intent intent = new Intent(".presenter.view.activity.ForgotPasswordCompleted");

   intent.putExtra("email", editEmail.getText().toString());


   ForgotPasswordFragment fragment = new ForgotPasswordFragment();
   Bundle bundle = new Bundle();
   bundle.putString("email",editEmail.getText().toString());
   fragment.setArguments(bundle);

startActivity(intent); }

表現:

<activity android:name=".presenter.view.activity.ForgotPasswordCompleted"
   android:label="Forgot"
   android:launchMode="singleTask"
   android:screenOrientation="portrait"
   android:windowSoftInputMode="adjustResize">

 <intent-filter>
   <action android:name=".presenter.view.activity.ForgotPasswordCompleted"/>
   <category android:name="android.intent.category.DEFAULT"/>
 </intent-filter>

我已經用Dagger綁定了textview,添加了字符串getName並在片段2中實現了intent和bundle:

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



    Intent intent = getActivity().getIntent();
        Bundle bd = intent.getExtras();
        if ((bd != null)){
            String getName = (String) bd.get("email");
            resentTxt.setText(getName);
        }

你忘了寫這行

InjectorClass.inject(this); 

在onCreate中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM