片段之间的动态通信失败

dynamic communication between fragments Fail

我正在尝试在两个 fragments 之间进行动态通信。 Frag1 实现 sensorlistener 并将值传递给 mainActivity

mainActivity 实现了 onSendListener.onSendValues 接口。在 mainActivityonSendValues 的实现中,当用户单击 Frag1 中定义的按钮时,我传递从 [=17] 接收到的加速度计 (x,y,z) 的值=] 通过 mainActivity 通过访问 Frag2 的 public 方法

Frag2
void returnedX(float x) {...}
void returnedY(float y) {...}
void returnedZ(float z) {...}

发生的情况是,当用户单击 Frag1 中的按钮时,Frag2 中的 textViews 尽管有 public 方法

但什么也不显示
void returnedX(float x) {...}
void returnedY(float y) {...}
void returnedZ(float z) {...}

被调用了,我确定它们被调用了,因为如下面的代码所示,显示了 Frag2 中 public 方法中的 Log 语句,但是加速度计没有。

请看下面的代码,让我知道为什么 Frag2 中的 public 方法不显示来自 Frag1

的传递日期

MainActivity:

public class MainActivity extends Activity implements onSendListener {

Frag2 frag2;

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

    frag2 = (Frag2)getFragmentManager().findFragmentById (R.id.frag_2);
}

/**
 * to send values from frag1 to frag2 through mainActivity. use bundle.
 */
@Override
public void onSendValues(float x, float y, float z) {
    // TODO Auto-generated method stub      
    frag2.returnedX(x);
    frag2.returnedY(y);
    frag2.returnedZ(z);
}

}

Frag1:

public class Frag1 extends Fragment implements SensorEventListener {

private SensorManager sensorManager;
TextView tvAccX;
TextView tvAccY;
TextView tvAccZ;

private float x = 0.0f;
private float y = 0.0f;
private float z = 0.0f;

private void setAccX(float x) {
    this.x = x;
}
private float getAccX() {
    return this.x;
}

private void setAccY(float y) {
    this.y = y;
}
private float getAccY() {
    return this.y;
}

private void setAccZ(float z) {
    this.z = z;
}
private float getAccZ() {
    return this.z;
}

Button btnSend;
onSendListener sendValues;

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);

    sendValues = (onSendListener) activity;
    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.frag_1, container, false);

    tvAccX = (TextView) view.findViewById(R.id.tv_accX_value);
    tvAccY = (TextView) view.findViewById(R.id.tv_accY_value);
    tvAccZ = (TextView) view.findViewById(R.id.tv_accZ_value);
    btnSend = (Button) view.findViewById(R.id.btn_send);

    return view;
}

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        showAccReadings(event);
        break;

    default:
        break;
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

private void showAccReadings(SensorEvent event) {
    // TODO Auto-generated method stub
    float[] values = event.values;

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

    setAccX(x);
    setAccY(y);
    setAccZ(z);

    tvAccX.setText(String.valueOf(x));
    tvAccY.setText(String.valueOf(y));
    tvAccZ.setText(String.valueOf(z));
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

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

    btnSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sendValues.onSendValues(getAccX(), getAccY(), getAccZ());
        }
    });
}

@Override
public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    sensorManager.unregisterListener(this);
}

}

Frag2:

public class Frag2 extends Fragment {

TextView tvX;
TextView tvY;
TextView tvZ;

float returnX ;
float returnY ;
float returnZ ;

void returnedX(float x) {
    if (getView() != null) {
        Log.d("Frag2", "X: View is not null");
        this.returnX = x;
    }
}
void returnedY(float y) {
    if (getView() != null) {
        Log.d("Frag2", "Y: View is not null");
        this.returnY = y;
    }
}
void returnedZ(float z) {
    if (getView() != null) {
        Log.d("Frag2", "Z: View is not null");
        this.returnZ = z;
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View view = inflater.inflate(R.layout.frag_2, container, false);

    tvX = (TextView) view.findViewById(R.id.tv_accX2_label);
    tvY = (TextView) view.findViewById(R.id.tv_accY2_label);
    tvZ = (TextView) view.findViewById(R.id.tv_accZ2_label);

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);

    Log.d("Frag2", "View is not null");
    tvX.setText("" + this.returnX);
    tvY.setText("" + this.returnY);
    tvZ.setText("" + this.returnZ);
}

}

日志输出:

01-26 13:03:36.876: D/Frag2(16146): X: View is not null
01-26 13:03:36.876: D/Frag2(16146): Y: View is not null
01-26 13:03:36.876: D/Frag2(16146): Z: View is not null

您需要将实际值设置为 TextViews

void returnedX(float x) {
    if (tvX != null) {
        tvX.setText("" + x);
    }
}