【问题标题】:communication between two fragments via interface fails两个片段之间通过接口的通信失败
【发布时间】:2016-02-09 12:17:13
【问题描述】:

我有一个包含两个选项卡的主要活动,每个选项卡都是一个片段(frag1 和 frag2)。 frag1 的布局有三个文本视图和一个按钮,frag1 实现SensorEventListener并在三个textview中显示加速度计值,按钮是将z轴的加速度计值传递给frag2。 Frag2 只有一个带有 textview 的布局来显示从 frag1 传递的值。

两个片段之间的通信是通过一个接口完成的,该接口由承载 frag1 和 frag2 的主 Activity 实现。在运行 当我按下 frag1 中的按钮将加速器值传递给 frag2 时,我收到 getView = null 尽管 frag2 已创建并且它的视图已准备好。

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

主要活动:

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

【问题讨论】:

  • this.mFrag2 = new Frag_2();。 mFrag2 的实例与 ViewPager 中加载的实例不同。获取加载的实例,一切都会好起来的。
  • @Rohit5k2 你能告诉我如何在 viewpager 中获取他的 lodaed 实例
  • 请看这个stackoverflow.com/questions/8785221/…快乐之路部分

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


【解决方案1】:

您可以使用 mRootView 值代替 getView() 来检查它是否为空。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多