0

I am trying to pass "firstname" from registerFragmentOne to registerFragmentTwo. But every time I start my application, it suddenly close. I am receiving this Error from Logcat:

at com.application.name.registerFragmentTwo.onCreateView

which is:

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

I declared Bundle on both fragments.

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 : (Where I load my fragments)

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);
}
}

Am I doing it right? I don't know why Bundle's value is gone after loading the second Fragment. I tried to Toast it before loading the second fragment, it has value before loading the next fragment.

Edit: The whole logcat error

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
halfer
  • 19,824
  • 17
  • 99
  • 186
Mon Pinoliad
  • 67
  • 1
  • 2
  • 11

4 Answers4

2

ViewPager creates and caches some of its fragments before they are displayed. In this case, the second fragment is created well before the user starts typing in the first fragment. This means that onCreateView() executes before you create the argument bundle.

To solve this, put the code for bundle.getString() in a different life cycle callback, such as onResume().

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Okay I tried putting Bundle get arguments and String frag1 = bundle.getString("firstname") inside onStart and onResume in registerFragmentTwo. But after loading the second frag, it is still null – Mon Pinoliad Feb 15 '18 at 16:01
  • @Transit To debug this, set break points or add `Log.d()` calls to see the order in which the lifecycle callbacks are executed in your main activity and in both fragments. You should also do the same in the relevant methods of your ViewPager adapter. – Code-Apprentice Feb 15 '18 at 16:42
  • @Transit Perhaps setting the text more directly [with something like this](https://stackoverflow.com/questions/48812064/how-to-change-my-fragments-text-by-menuitem-click) will work better than using a bundle. – Code-Apprentice Feb 15 '18 at 16:44
  • @Transit Also see the official Android docs for info about [Communicating between Fragments](https://developer.android.com/training/basics/fragments/communicating.html). – Code-Apprentice Feb 15 '18 at 16:45
0

Null pointer exception in your case occurs when the data in your bundle is empty. In your second fragment , first check whether the bundle is empty or not by using below snippet

Bundle bundle = getArguments():
 if(bundle !=null){
    // Do your work 
 }else{
   //Show error message
 }
Madhu Kumar
  • 175
  • 1
  • 4
  • 15
0

You need a new instance sttic builder to pass the argument

like this

public static MyFragment newInstance(String name, int age) {
    Bundle bundle = new Bundle();
    bundle.putString("name", name);
    bundle.putInt("age", age);

    MyFragment fragment = new MyFragment();
    fragment.setArguments(bundle);

    return fragment;
    }

or you can put args in your activity and do getActvity.getArgs() See this link https://gunhansancar.com/best-practice-to-instantiate-fragments-with-arguments-in-android/

Jack
  • 1,545
  • 2
  • 11
  • 20
0

(Posted on behalf of the question author).

The thing I forgot is to declare Bundle in my MainActivity which is RegisterActivity where my fragments are declared.

Communicating fragment to fragment should not pass data directly to the other fragment. There should be a interface Listener in order for activity to hold the data and pass it to the second fragment.

halfer
  • 19,824
  • 17
  • 99
  • 186