0

I have a main activity with two tabs each tab is a fragment (frag1 and frag2). the layout of frag1 has three textviews and a button,frag1 implements SensorEventListener and displays the accelerometer values in the three textviews, the button is to pass the accelerometer value of the z-axis to frag2. Frag2 has only one layout with a textview to display the passed value from frag1.

the communication between the two fragments is done via an interface implemented by the main activity that hosts both of frag1 and frag2. at run time when i press the button in frag1 to pass the acceleromter value to frag2 i receive getView = null despite frag2 is created and its view is ready.

below i posted the main activity, frag1 and frag2 , please help me to find out why every time i press the button to pass the value from frag1 to frag2 nothing is displayed and getView is null

main activity:

public class MainActivity extends AppCompatActivity implements IDataPasser{

private Toolbar mTB = null;
private TabLayout mTL = null;
private ViewPager mVP = null;
private VPagerAdapter mVPAdapter = null;

private Frag_2 mFrag2 = null;
private Frag_1 mFrag1 = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.initViews(R.layout.activity_main);
    this.initObjs();
}

private void initViews(int rootView) {
    setContentView(rootView);

    this.mTB = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(this.mTB);

    this.mTL = (TabLayout) findViewById(R.id.tab_layout);
    this.mTL.addTab(this.mTL.newTab().setText("Tab 1"));
    this.mTL.addTab(this.mTL.newTab().setText("Tab 2"));
    this.mTL.setTabGravity(TabLayout.GRAVITY_FILL);

    this.mVP = (ViewPager) findViewById(R.id.pager);
    this.mVP.setOffscreenPageLimit(1);
}

private void initObjs() {

    this.mFrag2 = new Frag_2();

    this.mVPAdapter = new VPagerAdapter(getSupportFragmentManager(), this.mTL.getTabCount());
    this.mVP.setAdapter(this.mVPAdapter);

    this.mVP.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(this.mTL));
    this.mTL.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mVP.setCurrentItem(tab.getPosition());
        }

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

        }

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

        }
    });
}

@Override
public void onValChanged(float val) {
    this.mFrag2.fromFrag1(val);
}
}

frag1:

public class Frag_1 extends Fragment implements SensorEventListener{

private final String TAG = this.getClass().getSimpleName();

private IDataPasser mPasser;

private View mRootView = null;
private TextView mtvAccX = null;
private TextView mtvAccY = null;
private TextView mtvAccZ = null;
private Button mbtnPass = null;

private SensorManager sensorManager;

float x;
float y;
float z;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    Log.w(TAG, "onAttach()");

    this.mPasser = (IDataPasser) context;
    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.w(TAG, "onCreate()");

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.w(TAG, "onCreateView()");

    this.mRootView = inflater.inflate(R.layout.frag_1, container, false);

    this.mtvAccX = (TextView) this.mRootView.findViewById(R.id.frag1_x);
    this.mtvAccY = (TextView) this.mRootView.findViewById(R.id.frag1_y);
    this.mtvAccZ = (TextView) this.mRootView.findViewById(R.id.frag1_z);
    this.mbtnPass = (Button) this.mRootView.findViewById(R.id.frag1_btn_pass);

    return this.mRootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Log.w(TAG, "onViewCreated()");
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.w(TAG, "onActivityCreated()");

    Sensor accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sensorManager.registerListener(this, accSensor, sensorManager.SENSOR_DELAY_FASTEST);

    this.mbtnPass.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPasser.onValChanged(z);
        }
    });
}

@Override
public void onStart() {
    super.onStart();
    Log.w(TAG, "onStart()");
}

@Override
public void onResume() {
    super.onResume();
    Log.w(TAG, "onResume()");
}

@Override
public void onPause() {
    super.onPause();
    Log.w(TAG, "onPause()");
}

@Override
public void onStop() {
    super.onStop();
    Log.w(TAG, "onStop()");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.w(TAG, "onDestroy()");
}

@Override
public void onSensorChanged(SensorEvent event) {

    switch (event.sensor.getType()) {
        case Sensor.TYPE_ACCELEROMETER:
            float[] values = event.values;

            x = values[0];
            y = values[1];
            z = values[2];

            this.mtvAccX.setText(String.valueOf(x));
            this.mtvAccY.setText(String.valueOf(y));
            this.mtvAccZ.setText(String.valueOf(z));
            break;

        default:
            break;
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
}

frag2:

public class Frag_2 extends Fragment {

private final String TAG = this.getClass().getSimpleName();

private View mRootView = null;
private TextView mtvText = null;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    Log.w(TAG, "onAttach()");
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.w(TAG, "onCreate()");
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.w(TAG, "onCreateView()");

    this.mRootView = inflater.inflate(R.layout.frag_2, container, false);
    this.mtvText = (TextView) this.mRootView.findViewById(R.id.frag2_tv_z);

    Log.v(TAG, "getView(): " + getView());
    return mRootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Log.w(TAG, "onViewCreated()");

    Log.v(TAG, "getView(): " + getView());
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.w(TAG, "onActivityCreated()");
}

@Override
public void onStart() {
    super.onStart();
    Log.w(TAG, "onStart()");
}

@Override
public void onResume() {
    super.onResume();
    Log.w(TAG, "onResume()");
}

@Override
public void onPause() {
    super.onPause();
    Log.w(TAG, "onPause()");
}

@Override
public void onStop() {
    super.onStop();
    Log.w(TAG, "onStop()");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.w(TAG, "onDestroy()");
}

public void fromFrag1(float val) {
    if (getView() != null) {
        this.mtvText.setText(""+val);
    } else {
        Log.e(TAG, "getView = null");
    }
}
}
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • `this.mFrag2 = new Frag_2();`. `mFrag2` has a different instance than what is loaded in the `ViewPager`. Get the loaded instance and everything will be fine. – Rohit5k2 Feb 09 '16 at 12:22
  • @Rohit5k2 can u please tell me how to get he lodaed instance in the viewpager – Amrmsmb Feb 09 '16 at 12:26
  • Please see this http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager/24662049#24662049 The happy path part – Rohit5k2 Feb 09 '16 at 12:43

1 Answers1

0

Instead of getView() you can use mRootView value to check whether it is null or not.

check this answer https://stackoverflow.com/a/9505298/1320616

Community
  • 1
  • 1
Ankit Aggarwal
  • 5,317
  • 2
  • 30
  • 53