我正在尝试在两个fragments
之间动态通信。 Frag1
imlements sensorlistener
并将值传递给mainActivity
。
mainActivity
实现onSendListener.onSendValues
接口。在onSendValues
中实现mainActivity
时,当用户点击Frag1
中定义的按钮时,我会传递从{{1}收到的加速度计(x,y,z)的值通过Frag1
的公共方法访问Frag2
到mainActivity
Frag2
当用户点击void returnedX(float x) {...}
void returnedY(float y) {...}
void returnedZ(float z) {...}
中的按钮时,Frag1
中的textViews
会显示任何内容,而不管其公开方法
Frag2
被调用,我确信它们被调用,因为如下面的代码所示,void returnedX(float x) {...}
void returnedY(float y) {...}
void returnedZ(float z) {...}
中的公共方法中的Log
语句被显示,但是加速度计的值没有。
请参阅下面的代码,并告诉我为什么Frag2
中的公开方法不会显示Frag2
MainActivity :
Frag1
}
Frag1 :
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);
}
}
Frag2 :
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);
}
}
日志输出:
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);
}
答案 0 :(得分:1)
您需要将实际值设置为TextViews
void returnedX(float x) {
if (tvX != null) {
tvX.setText("" + x);
}
}