Android : Update Tab Layout(fragments) textviews from an activity
<p><a href="https://i.stack.imgur.com/Gety4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gety4.png" alt="Tabs"></a></p>

<p>I'm creating tab view using tablayout and viewpager. I use one fragment for each tab. And in my MainActivity, I'm getting some values from a device through bluetooth. The values are received as a single string, and I split it and sets it to corresponding tabs. But I'm getting a NullPointerException when I try to call the setText method for the textview in fragment from my activity.
Can anybody show me how to update the textviews of each of the fragments from my MainActivity?? Like, if i select the temperature tab, the textview in the temperature fragment should be updated with the value from MainActivity. Please Help</p>

<p>Activity</p>

<pre><code>    public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {

   Handler bluetoothIn;
   private static TextView tmpF, humF, CoF;
   String tempGL, HumGL, coGL, devname;
   double dblTemp, dblCo;
   final int handlerState = 0; // used to identify handler message
   private BluetoothAdapter btAdapter = null;
   private TabLayout tabLayout;
   private ViewPager viewPager;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_frag_home);

 tmpF = (TextView)findViewById(R.id.txt_temp_val);
 humF = (TextView)findViewById(R.id.txt_hum_val);
 co = (TextView)findViewById(R.id.txt_co_val);

//Adding toolbar to the activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing the tablayout
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.pager);
//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setText("Temperature"));
tabLayout.addTab(tabLayout.newTab().setText("Humidity"));
tabLayout.addTab(tabLayout.newTab().setText("CO"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

//Creating our pager adapter
Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());

//Adding adapter to pager
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//Adding onTabSelectedListener to swipe views
tabLayout.setOnTabSelectedListener(this);

bluetoothIn=new Handler() {

    String readMessage;
    String[] values = new String[]{""};

    public void handleMessage(android.os.Message msg) {

        if (msg.what == handlerState) {

            readMessage = (String) msg.obj;
            values = readMessage.split("#");
            for (int j = 0; j &lt; values.length; j++) {

                int rem = j % 3;

                if (rem == 0) {

                    tmpF.setText(values[j] + " C");
                    tempGL = String.valueOf(values[j]);

                    try {
                        dblTemp = Double.parseDouble(tempGL);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }

                } else if (rem == 1) {

                    CoF.setText(values[j] + " ppm");
                    coGL = values[j];
                    try {
                        dblCo = Double.parseDouble(coGL);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }

                } else if (rem == 2) {
                    humF.setText(values[j] + " %");
                    HumGL = values[j];
                }
            }
        }
    }
};

btAdapter=BluetoothAdapter.getDefaultAdapter(); // get Bluetooth
}

 @Override
 public void onTabSelected(TabLayout.Tab tab) {
// mViewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

 @Override
 public void onTabReselected(TabLayout.Tab tab) {

 }
 }
</code></pre>

<p>Tab1 </p>

<pre><code>  public class OneFragment extends Fragment {

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

 }
 }
</code></pre>

<p>fragment_one.xml</p>

<pre><code> &lt;RelativeLayout 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"
 tools:context="shinil.tablayout.OneFragment"
 android:id="@+id/rltnvnv"&gt;

&lt;TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Temperature"
android:textSize="40dp"
android:textStyle="bold"
android:id="@+id/txt_temp_val"
android:layout_centerInParent="true"/&gt;

&lt;/RelativeLayout&gt;
</code></pre>

<p>Logcat</p>

<pre><code>  Caused by: java.lang.NullPointerException: Attempt to invoke virtual       method 'void       android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference at   shinil.airopure.MainActivity.onCreate(MainActivity.java:194)
</code></pre>

<p>frag_home.xml</p>

<pre><code>    &lt;LinearLayout
    android:id="@+id/main_layout"
    android:orientation="vertical"
    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=".MainActivity"&gt;


  &lt;android.support.v7.widget.Toolbar
 android:id="@+id/toolbar"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="?attr/colorPrimary"
 android:minHeight="?attr/actionBarSize"
 android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
 app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/&gt;


  &lt;android.support.design.widget.TabLayout
  android:id="@+id/tabLayout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="?attr/colorPrimary"

  android:minHeight="?attr/actionBarSize"
  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/&gt;


 &lt;android.support.v4.view.ViewPager
  android:id="@+id/pager"
  android:layout_width="match_parent"
 android:layout_height="fill_parent"/&gt;

&lt;/LinearLayout&gt;
</code></pre>