title: Passing data between two tab fragments in a SlidingTabslayout
body: <p>I have an application which has one main activity which in turn hosts two tabs. Each tab is a fragment. The first tab has an editText and a Button and on button press the text entered by the user must be displayed on the second tab fragment which hosts only one textView. I am using a listener to pass the data but for some reason the app crashes when i press the submit button. Any help would be appreciated. Here is my code:</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>    public class MainActivity extends AppCompatActivity implements OnEditTextListener {
  
      Toolbar toolbar;
      ViewPager pager;
      ViewPagerAdapter adapter;
      SlidingTabLayout tabs;
      CharSequence Titles[] = {"Home", "Events"};
      int Numboftabs = 2;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Creating The Toolbar and setting it as the Toolbar for the activity
  
          toolbar = (Toolbar) findViewById(R.id.tool_bar);
          setSupportActionBar(toolbar);
  
  
          // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
          adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
  
          // Assigning ViewPager View and setting the adapter
          pager = (ViewPager) findViewById(R.id.pager);
          pager.setAdapter(adapter);
  
          // Assiging the Sliding Tab Layout View
          tabs = (SlidingTabLayout) findViewById(R.id.tabs);
          tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
  
          // Setting Custom Color for the Scroll bar indicator of the Tab View
          tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
              @Override
              public int getIndicatorColor(int position) {
                  return getResources().getColor(R.color.ColorPrimary);
              }
          });
  
          // Setting the ViewPager For the SlidingTabsLayout
          tabs.setViewPager(pager);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      public void OnEditText(String data) {
          Tab2 newFragment = (Tab2)adapter.getItem(1);
          newFragment.updateDisplay(data);
      }
  }
  </code></pre>
  
  <p>Tab1.java</p>
  
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.text.Editable;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.EditText;
  
  public class Tab1 extends Fragment
  {
      public interface OnEditTextListener {
          public void OnEditText(String data);
      }
  
      OnEditTextListener editTextListener = null;
      EditText edtText;
      Button btnSubmit;
  
      String val;
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
          try {
              editTextListener = (OnEditTextListener) activity;
          } catch (ClassCastException ex) {
              ex.printStackTrace();
          }
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.first_fragment, container, false);
          return v;
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          edtText = (EditText)getActivity().findViewById(R.id.edtText);
          btnSubmit = (Button)getActivity().findViewById(R.id.btnSubmit);
          btnSubmit.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  val = edtText.getText().toString();
                  editTextListener.OnEditText(val);
              }
          });
      }
  }
  </code></pre>
  
  <p>Tab2.java </p>
  
  <pre><code>public class Tab2 extends Fragment {
  
      public static final String TAG = "DATA";
      public static final String SAVE = "NBHJB";
      String newdata = "";
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          if (savedInstanceState != null)
              savedInstanceState.getInt(SAVE);
  
          return inflater.inflate(R.layout.second_fragment, container, false);
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          if (savedInstanceState != null) {
              savedInstanceState.getString(SAVE);
          }
      }
  
      public void updateDisplay(String data) {
          newdata = data;
          TextView randomData = (TextView) getActivity().findViewById(R.id.txtView1);
          randomData.setText(newdata);
      }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
  
          outState.putString(SAVE, newdata);
      }
  </code></pre>
  
  <p>MainActivity.xml</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      tools:context=".MainActivity"&gt;
  
      &lt;include
          android:id="@+id/tool_bar"
          layout="@layout/tool_bar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" /&gt;
  
      &lt;View
          android:layout_height="2dip"
          android:background="@color/tabsScrollColor"
          android:layout_width="wrap_content" /&gt;
  
      &lt;com.example.regi.tabsprove.SlidingTabLayout
          android:id="@+id/tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/prove"
          android:elevation="2dp" /&gt;
  
  
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"&gt;&lt;/android.support.v4.view.ViewPager&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>second_fragment.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal"&gt;
  
      &lt;TextView
          android:id="@+id/txtView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:gravity="center"
          android:padding="10dp"
          android:text="Default Text"
          android:textSize="20dp" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>This is the error that I get</p>
  
  <pre><code>java.lang.NullPointerException
              at com.example.regi.tabsprove.Tab2.updateDisplay(Tab2.java:36)
              at com.example.regi.tabsprove.MainActivity.OnEditText(MainActivity.java:79)
              at com.example.regi.tabsprove.Tab1$1.onClick(Tab1.java:51)
              at android.view.View.performClick(View.java:4198)
              at android.view.View$PerformClick.run(View.java:17158)
              at android.os.Handler.handleCallback(Handler.java:615)
              at android.os.Handler.dispatchMessage(Handler.java:92)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4918)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>