Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

android - Why bundle is null in dialog?

Hello im passing bundle between two dialogs. In my first dialog i put the strings inside a bundle and they are not null. In my second dialog i try to take the bundle and put them in strings but the bundle is null. I managed sendling bundles before but now with the same code it dont work and is null in the second fragment. What is wrong?

First Fragment

EditText userName,emailAddress,password,repeat;
Button register;
String isntequal;
String pwtoshort;
String wrongEmail;


private static final String TAG = "Register";

TextView openlog, wrongpw, wrongemail;

public static void display(FragmentManager fragmentManager) {
    Registration registration = new Registration();
   registration.show(fragmentManager, TAG);
}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme_FullScreenDialog);

}

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

    wrongEmail= getString(R.string.wrongemail);
    pwtoshort=getString(R.string.pwtoshort);
    isntequal= getString(R.string.isntequal);


    wrongemail= view.findViewById(R.id.wrongemail);
    wrongpw= view.findViewById(R.id.isntequals);
    userName = view.findViewById(R.id.name);
    emailAddress = view.findViewById(R.id.EmailAddress);
    password = view.findViewById(R.id.Password);
    repeat = view.findViewById(R.id.repeatPassword);
    register = view.findViewById(R.id.createaccout);

    userName.addTextChangedListener(loginTextWatcher);
    emailAddress.addTextChangedListener(loginTextWatcher);
    password.addTextChangedListener(loginTextWatcher);
    repeat.addTextChangedListener(loginTextWatcher);


    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String username = userName.getText().toString();
            final String email = emailAddress.getText().toString();
            final String Password = password.getText().toString();
            final String Repeat = repeat.getText().toString();
            if(!(Password.equals(Repeat))){
                wrongpw.setText(isntequal);
                wrongpw.setVisibility(View.VISIBLE);

            }
            else if (Password.length()< 8){
                wrongpw.setText(pwtoshort);
                wrongpw.setVisibility(View.VISIBLE);

            }

            else if (!isEmailValid(email)){
                wrongemail.setText(wrongEmail);
                wrongemail.setVisibility(View.VISIBLE);
            }

            else {
                dismiss();
                Bundle bundle=  new Bundle();
                accountverify accountVerify = new accountverify();
                bundle.putString("username", username);
                bundle.putString("email", email);
                Log.d(TAG, "onClick: Hello"+bundle);
                accountverify.display(getActivity().getSupportFragmentManager());
                accountVerify.setArguments(bundle);



            }
        }

    });

My second Fragment

 private static final String TAG = "verify";
String Name;
String Email;

private String verifytext1;
private String verifytext2;
private String verifytext3;
private String verifytext4;
private String verifytextausruf;


private TextView verifytextview;
private TextView verifytextview2;
private Button repeatsend;

public static void display(FragmentManager fragmentManager) {
    accountverify accountVerify = new accountverify();
    accountVerify.show(fragmentManager, TAG);
}


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle arg = getArguments();
    if (arg!=null){
        Name = arg.getString("userName");
        Email = arg.getString("email");
    }
    verifytext1= getString(R.string.verifytext1);
    verifytextausruf= getString(R.string.ausruf);
    verifytext2= getString(R.string.verifytext2);
    verifytext3= getString(R.string.verifytext3);
    verifytext4= getString(R.string.verifytext4);
}

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

    verifytextview = view.findViewById(R.id.verifytext);
    verifytextview2 = view.findViewById(R.id.verifytext2);


    if(Name!=null && Email!= null){
        verifytextview.setText(verifytext1+Name+verifytextausruf+verifytext2+Email+verifytext3);
    }
    else verifytextview.setText(
            "Vielen Dank für deine Registrierung und herzlich Willkommen! Dein NightStar Account wurde so eben erstellt. Um deinen Account zu verifizieren, bitten wir dich mit dem Best?tigungslink in der E-Mail, die wir Dir geschickt haben deinen Account zu best?tigen.");

    verifytextview2.setText(verifytext4);

    Button closebutton = view.findViewById(R.id.verify_exit);
    closebutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });
    return view;
}


@Override
public void onStart() {
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null) {
        int width = ViewGroup.MarginLayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(width, height);
        ColorDrawable back = new ColorDrawable(Color.TRANSPARENT);
        InsetDrawable inset = new InsetDrawable(back, 0, 60, 0, -10);
        dialog.getWindow().setBackgroundDrawable(inset);
        dialog.getWindow().setWindowAnimations(R.style.AppTheme_Slide);
    }
}
question from:https://stackoverflow.com/questions/65953058/why-bundle-is-null-in-dialog

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to reply this article.

OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...