我正在使用计步器来计算自上次重启设备以来的步数。
我创建了一个textView,我想将文本设置为传感器使用时的步数。 (我的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: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>
答案 0 :(得分:2)
更改
private TextView textView;
到
private TextView count;
并改变
TextView count = (TextView) getActivity().findViewById(R.id.count);
到
count = (TextView) getActivity().findViewById(R.id.count);
答案 1 :(得分:1)
将“count”从“onCreateView”中删除,因此它可以在“onSensorChanged”中访问
public class Tab3 extends Fragment implements SensorEventListener {
private SensorManager sensorManager;
private TextView textView;
boolean activityRunning;
private Button buttonReturn;
private TextView count ;//<-- add this
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab3, container, false);
//define count global
count = (TextView)rootView.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) {
}
}
答案 2 :(得分:0)
您是如何定义&#34;计数&#34;?
的如果我们假设您已正确定义TextView,则会在null对象中出错, 因此最好将代码更改为:
count.setText(""+event.values[0]);