gpt4 book ai didi

android - getArgument() 返回 NULL

转载 作者:行者123 更新时间:2023-12-05 00:07:04 25 4
gpt4 key购买 nike

在下面的代码中,我尝试将传感器值从 Frag1 传递到 Frag2通过 mainActivityFrag1 实现了 sensorEvenListener,在 Frag1onResume 中,我通过 sendValues 将值传递给 mainActivtiy。 onSendValues(getAccX(), getAccY(), getAccZ()); sendValues 是在 中实现的名为 onSendListener 的接口(interface)对象>mainActivity.

MainActivity 中,我使用

将数据传递给 Frag2
@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 的包是否为 null 时,我发现它是 null,为什么它是 null?

请找到下面发布的代码

** 主要 Activity **:

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

** fragment 1 **:

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

}

** fragment 2 **:

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

if (getArguments() != null) {
Log.d("Frag2", "getArgument() is not NULL");
x = getArguments().getFloat("x");
y = getArguments().getFloat("y");
z = getArguments().getFloat("z");
} else {
Log.d("Frag2", "getArgument() is NULL");
}

tvX.setText(String.valueOf(x));
tvY.setText(String.valueOf(y));
tvZ.setText(String.valueOf(z));
}
}

** mainActivity.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

第一个是在 mainActivity.xml 的膨胀期间创建的,并且在执行其 onCreateView 或 onViewCreated 方法时没有参数传递给它。

第二个在您的代码中实例化如下:

Frag2 frag2 = new Frag2();
frag2.setArguments(b);

因此,您在此处设置的参数与附加到您的 Activity 的先前参数无关。

这里是你问题的解决方案:

  1. 将公共(public)方法添加到您的 Frag2 类,如下所示:
   public void passValues(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
View view = getView();
if(view != null){
// set the value to views here.
}

}
  1. 如下更改 MainActivity 中的 onSendValues 方法:
@Override
public void onSendValues(float x, float y, float z) {
Frag2 frag2 = (Frag2)getFragmentManager().findFragmentById (R.id.frag_2);
frag2.passValues(x, y, z);
}

关于android - getArgument() 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28148440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com