安卓无法解析片段中的方法setText(java.lang.String)
我正在使用计步器计算设备上次重新启动后的步数
我已经创建了一个textView,我想将它的文本设置为使用传感器时的步数。(我的文本视图称为计数)
我的代码是:
@Override
public void onSensorChanged(SensorEvent event) {
if (activityRunning) {
count.setText(String.valueOf(event.values[0]));
}
我在这里得到一个错误Cannot resolve method setText(java.lang.String)
如果有助于解决这个问题,我可以发布课堂上的其他内容
谢谢
这是本课程的其余部分:
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:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.application.placepicker.TabbedActivity$PlaceholderFragment">
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical"
安卓:layout_marginTop="114dp"
安卓:gravity="center"
安卓:layout_alignParentTop="true"
安卓:layout_alignParentStart="true">
<TextView
安卓:id="@+id/textLogin"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="center_horizontal"
安卓:text="Step counter"
安卓:textSize="40sp"
安卓:textColor="@color/colorAccent"
安卓:fontFamily="sans-serif"
安卓:textAppearance="?安卓:attr/textAppearanceLarge" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="33dp"
安卓:layout_gravity="center_horizontal"
安卓:textSize="26dp"
安卓:text="Steps walked so far today:"
安卓:textColor="@color/colorAccent" />
<TextView
安卓:id="@+id/count"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="40dp"
安卓:layout_marginLeft="50dp"
安卓:text="---"
安卓:textColor="@color/colorAccent"
安卓:textSize="36dp" />
</LinearLayout>
# 1 楼答案
将“count”从“onCreateView”中带出,以便在“onSensorChanged”中可以访问它
# 2 楼答案
你如何定义你的“计数”
如果我们假设您已正确定义TextView,则会在null对象中出现错误, 因此,最好将代码更改为以下内容:
# 3 楼答案
改变
到
改变
到