在下面的代码中,我有一个mainActivity,主持两个fragments
Frag1
和Frag2
,如下所示,Frag1
实现sensorEventListener
,当{单击button
中的{1}},传感器读数应传递到Frag1
。
为此,我创建了一个Frag2
中的onSendListener
接口设置为onAttch
。 sendValues = (onSendListener) activity;
实施mainActivity
,如下面发布的onSendListener
代码所示,mainActivity
我将读数传递给onSendListener
。
在运行时,我收到以下发布的Frag2
错误。
请让我知道我错过了什么错误。
** MainActivity **:
logCat
}
** Frag1 **:
public class MainActivity extends Activity implements onSendListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
/**
* 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
Bundle b = new Bundle();
b.putFloat("x", x);
b.putFloat("y", y);
b.putFloat("z", z);
Frag2 frag2 = new Frag2();
frag2.setArguments(b);
}
}
** 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 onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
showAccReadings(event);
break;
default:
break;
}
}
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 onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@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);
}
}
** mainActivity.xml **:
public class Frag2 extends Fragment {
float x;
float y;
float z;
TextView tvX;
TextView tvY;
TextView tvZ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
x = getArguments().getFloat("x");
y = getArguments().getFloat("y");
z = getArguments().getFloat("z");
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);
tvX.setText(String.valueOf(x));
tvY.setText(String.valueOf(y));
tvZ.setText(String.valueOf(z));
}
** Frag1.xml **:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}">
<fragment
android:name="com.example.fragmentcommunication_00.Frag1"
android:id="@+id/frag_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<fragment
android:name="com.example.fragmentcommunication_00.Frag2"
android:id="@+id/frag_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/frag_1"/>
** Frag2.xml **:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_accX_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="Acc[X]: "/>
<TextView
android:id="@+id/tv_accX_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tv_accX_label"
android:gravity="center_vertical|center_horizontal"/>
<TextView
android:id="@+id/tv_accY_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@id/tv_accX_label"
android:text="Acc[Y]: "/>
<TextView
android:id="@+id/tv_accY_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/tv_accY_label"
android:layout_below="@id/tv_accX_label"
android:gravity="center_vertical|center_horizontal"/>
<TextView
android:id="@+id/tv_accZ_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@id/tv_accY_label"
android:text="Acc[Z]: "/>
<TextView
android:id="@+id/tv_accZ_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/tv_accZ_label"
android:layout_below="@id/tv_accY_label"
android:gravity="center_vertical|center_horizontal"/>
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_accZ_label"
android:gravity="center_vertical|center_horizontal"
android:text="send to frag_2"/>
logcat的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_accX2_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="Acc[X]: "/>
<TextView
android:id="@+id/tv_accY2_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/tv_accX2_label"
android:text="Acc[Y]: "/>
<TextView
android:id="@+id/tv_accZ2_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/tv_accY2_label"
android:text="Acc[Z]: "/>
答案 0 :(得分:0)
显然系统无法创建第二个片段,因为它需要您在不同时间设置的参数,而不是在创建布局时。 ActivityManager在您的Activity中调用onCreate(),在片段中调用onCreate()和onCreateView()。但是调用方法onSendValues()设置参数不会在创建时发生。
设置片段的参数不是片段之间动态通信的解决方案。它仅适用于初始创建视图
您需要在第二个片段中创建公共方法,并在需要将活动数据传递给活动时从活动中调用它。因此,通信方案是:Fragment1通过活动中的接口调用方法 - &gt; Fragment2中的活动调用方法