gpt4 book ai didi

java - 无法解析 Fragment 中的 setText(java.lang.String) 方法

转载 作者:行者123 更新时间:2023-12-01 20:04:03 26 4
gpt4 key购买 nike

我正在使用计步器来计算自上次重新启动设备以来的步数。

我创建了一个 TextView ,我想将其文本设置为使用传感器时的步数。 (我的TextView叫做count)

我执行此操作的代码是:

@Override
public void onSensorChanged(SensorEvent event) {
if (activityRunning) {
count.setText(String.valueOf(event.values[0]));
}

我在这里收到错误无法解析方法setText(java.lang.String)

如果有助于解决问题,我可以发布类(class)的其余部分

谢谢

这是类(class)的其余部分:

public class Tab3 extends Fragment implements SensorEventListener {

private SensorManager sensorManager;
private TextView textView;
boolean activityRunning;
private Button buttonReturn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab3, container, false);

TextView count = (TextView) getActivity().findViewById(R.id.count);

sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);

getActivity().getSystemService(Context.SENSOR_SERVICE);

return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}

@Override
public void onResume() {
super.onResume();
activityRunning = true;
Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if (countSensor != null) {
sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
} else {
Toast.makeText(getActivity(), "Count sensor not available!", Toast.LENGTH_LONG).show();
}

}

@Override
public void onPause() {
super.onPause();
activityRunning = false;
}

@Override
public void onSensorChanged(SensorEvent event) {
if (activityRunning) {
count.setText(String.valueOf(event.values[0]));
}

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
}

这是与此类关联的 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.application.placepicker.TabbedActivity$PlaceholderFragment">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="114dp"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">

<TextView
android:id="@+id/textLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Step counter"
android:textSize="40sp"
android:textColor="@color/colorAccent"
android:fontFamily="sans-serif"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:layout_gravity="center_horizontal"
android:textSize="26dp"
android:text="Steps walked so far today:"
android:textColor="@color/colorAccent" />

<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="50dp"
android:text="---"
android:textColor="@color/colorAccent"
android:textSize="36dp" />

</LinearLayout>

最佳答案

改变

private TextView textView;

private TextView count;

并改变

TextView count = (TextView) getActivity().findViewById(R.id.count);

count = (TextView) getActivity().findViewById(R.id.count);

关于java - 无法解析 Fragment 中的 setText(java.lang.String) 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47679142/

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