我试图将“名字”从registerFragmentOne传递到registerFragmentTwo。但是每次我启动应用程序时,它突然关闭。我从Logcat收到此错误:


  在com.application.name.registerFragmentTwo.onCreateView


这是:

String frag1 = bundle.getString("firstname");


我在两个片段上都声明了捆绑。

registerFragmentOne:

package com.sholomon.who.who;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class registerFragmentOne extends Fragment {
private Button fButton;
private EditText fName;
private TextView fTextView;

private Animation shake;
private boolean flagAnimate = false;
private final Animation in = new AlphaAnimation(0.0f , 1.0f);
private final Animation out = new AlphaAnimation(1.0f, 0.0f);

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_register_fragment_one, container, false);
    fButton = (Button) view.findViewById(R.id.fButton);
    fName = (EditText) view.findViewById(R.id.fName);
    fTextView = (TextView) view.findViewById(R.id.fTextView);

    // Animation List
    shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
    in.setDuration(500);
    out.setDuration(500);

    fName.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String getName = fName.getText().toString();
            int getNameCount = getName.replaceAll("\\s+","").length();
            if(getNameCount >= 1 && !flagAnimate) {
                fTextView.startAnimation(in);
                fTextView.setVisibility(View.VISIBLE);
                flagAnimate= true;
            } else if(getNameCount == 0){
                fTextView.startAnimation(out);
                fTextView.setVisibility(View.INVISIBLE);
                flagAnimate= false;
            }

        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged (Editable s){
            // TODO Auto-generated method stub
        }

    });

    fButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(fName.length() < 1) {
                fName.startAnimation(shake);
            } else {
                String firstName = fName.getText().toString();

                Bundle bundle = new Bundle();
                bundle.putString("firstname", firstName);

                registerFragmentTwo fragobj = new registerFragmentTwo();
                fragobj.setArguments(bundle);

                Toast.makeText(getActivity(), bundle.getString("firstname"), Toast.LENGTH_LONG).show();
                ((RegisterActivity)getActivity()).setViewPager(1);
            }
        }
    });

    return view;
}
}


registerFragmentTwo:

package com.sholomon.who.who;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

public class registerFragmentTwo extends Fragment {

private Button mButton;
private EditText mName;
private TextView mTextView, nameViewOne;

private String frag1;

private Animation shake;
private boolean flagAnimate = false;
private final Animation in = new AlphaAnimation(0.0f , 1.0f);
private final Animation out = new AlphaAnimation(1.0f, 0.0f);

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

    mButton = (Button) view.findViewById(R.id.mButton);
    mName = (EditText) view.findViewById(R.id.mName);
    mTextView = (TextView) view.findViewById(R.id.mTextView);

    // Animation List
    shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
    in.setDuration(500);
    out.setDuration(500);

    Bundle bundle = getArguments();

    nameViewOne = (TextView) view.findViewById(R.id.nameViewOne);
    String frag1 = bundle.getString("firstname");
    nameViewOne.setText(frag1 + " ...");

    mName.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String getName = mName.getText().toString();
            int getNameCount = getName.replaceAll("\\s+","").length();
            if(getNameCount >= 1 && !flagAnimate) {
                mTextView.startAnimation(in);
                mTextView.setVisibility(View.VISIBLE);
                flagAnimate= true;
            } else if(getNameCount == 0){
                mTextView.startAnimation(out);
                mTextView.setVisibility(View.INVISIBLE);
                flagAnimate= false;
            }

        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged (Editable s){
            // TODO Auto-generated method stub
        }

    });

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(mName.length() < 1) {
                mName.startAnimation(shake);
            } else {
                //GlobalClass.firstName = mName.getText().toString();
                ((RegisterActivity) getActivity()).setViewPager(0);
            }
        }
    });

    return view;
}
}


registerFragmentTwo XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sholomon.who.who.registerFragmentTwo">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/nameViewOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/mTextView"
        android:layout_alignStart="@+id/mTextView"
        android:layout_marginBottom="53dp"
        android:textSize="25dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/mTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/mName"
        android:layout_alignStart="@+id/mName"
        android:text="First Name"
        android:visibility="invisible" />

    <EditText
        android:id="@+id/mName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/mButton"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="126dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/edittext"
        android:ems="10"
        android:hint="Middle Name"
        android:inputType="textCapSentences"
        android:paddingBottom="15dp"
        android:paddingTop="15dp"
        android:textSize="20dp" />

    <Button
        android:id="@+id/mButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="12dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/button_register" />

</RelativeLayout>
</android.support.constraint.ConstraintLayout>


RegisterActivity :(在我加载片段的位置)

public class RegisterActivity extends AppCompatActivity {


private FirebaseAuth mAuth;
private FirebaseUser firebaseUser;

private DatabaseReference mDatabase;

private SectionsStatePagerAdapter sectionsStatePagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    setContentView(R.layout.activity_register);

    mAuth = FirebaseAuth.getInstance();
    firebaseUser = mAuth.getCurrentUser();
    mDatabase = FirebaseDatabase.getInstance().getReference();

    mViewPager = (ViewPager) findViewById(R.id.mainContainer);
    // Setup Adapter
    setupViewPager(mViewPager);
}

private void setupViewPager(ViewPager viewPager) {
    SectionsStatePagerAdapter adapter = new SectionsStatePagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new registerFragmentOne(), "First Name");
    adapter.addFragment(new registerFragmentTwo(), "Second Name");
    viewPager.setAdapter(adapter);
}

// Will be accessed from outside in order to navigate on the next item
public void setViewPager(int fragmentNumber) {
    mViewPager.setCurrentItem(fragmentNumber);
}
}


我做对了吗?我不知道为什么加载第二个Fragment后Bundle的值消失了。我尝试在加载第二个片段之前敬酒它,在加载下一个片段之前它具有价值。

编辑:整个logcat错误

FATAL EXCEPTION: main
                                                                Process: com.application.name, PID: 8683
                                                                java.lang.NullPointerException
                                                                    at com.sholomon.application.name.onCreateView(registerFragmentTwo.java:50)
                                                                    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
                                                                    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
                                                                    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
                                                                    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
                                                                    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
                                                                    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
                                                                    at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199)
                                                                    at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
                                                                    at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167)
                                                                    at android.support.v4.view.ViewPager.populate(ViewPager.java:1236)
                                                                    at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
                                                                    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
                                                                    at android.view.View.measure(View.java:16834)
                                                                    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:824)
                                                                    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:500)
                                                                    at android.view.View.measure(View.java:16834)
                                                                    at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
                                                                    at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
                                                                    at android.view.View.measure(View.java:16834)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
                                                                    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                    at android.view.View.measure(View.java:16834)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
                                                                    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
                                                                    at android.view.View.measure(View.java:16834)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
                                                                    at android.view.View.measure(View.java:16834)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
                                                                    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1621)
                                                                    at android.widget.LinearLayout.measureVertical(LinearLayout.java:742)
                                                                    at android.widget.LinearLayout.onMeasure(LinearLayout.java:607)
                                                                    at android.view.View.measure(View.java:16834)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
                                                                    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2333)
                                                                    at android.view.View.measure(View.java:16834)
                                                                    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2246)
                                                                    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1312)
                                                                    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1509)
                                                                    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)
                                                                    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223)
                                                                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
                                                                    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
                                                                    at android.view.Choreographer.doFrame(Choreographer.java:560)
                                                                    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
                                                                    at android.os.Handler.handleCallback(Handler.java:808)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                    at android.os.Looper.loop(Looper.java:193)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                    at java.lang.reflect.Method.invokeNative(Native Meth

最佳答案

ViewPager创建并缓存其某些片段,然后将其显示。在这种情况下,将在用户开始输入第一个片段之前创建第二个片段。这意味着onCreateView()在创建参数束之前执行。

要解决此问题,请将bundle.getString()的代码放入其他生命周期回调中,例如onResume()

10-07 20:54