通过接口的两个片段之间的通信失败

时间:2016-02-09 12:17:14

标签: android android-fragments android-viewpager fragment android-tabs

我有一个带有两个标签的主要活动,每个标签都是一个片段(frag1和frag2)。 frag1的布局有三个textview和一个按钮,frag1 实现SensorEventListener并在三个textview中显示加速度计值,该按钮用于将z轴的加速度计值传递给frag2。 Frag2只有一个带有textview的布局,用于显示frag1传递的值。

两个片段之间的通信是通过托管frag1和frag2的主要活动实现的接口完成的。在奔跑 当我按下frag1中的按钮将加速器值传递给frag2时,我接收到getView = null,尽管创建了frag2并且其视图已准备就绪。

下面我发布了主要活动,frag1和frag2,请帮我找出为什么每次按下按钮将值从frag1传递给frag2都没有显示和 getView为null

主要活动

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

1 个答案:

答案 0 :(得分:0)

您可以使用mRootView值来检查它是否为null,而不是getView()。

检查此答案https://stackoverflow.com/a/9505298/1320616