我正在尝试在片段中使用计步器传感器来计算自上次重启设备以来所采取的步数。
我可以让计步器在Activity
中工作但似乎无法在fragment
中使用它。我是新手使用碎片,所以我做错了可能非常简单。
我的课程如下:
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import static com.example.application.placepicker.R.id.count;
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);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
count = (TextView) getActivity().findViewById(count); **error
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); **error
}
@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(this, "Count sensor not available!", Toast.LENGTH_LONG).show(); **error
}
}
@Override
public void onPause() {
super.onPause();
activityRunning = false;
}
@Override
public void onSensorChanged(SensorEvent event) {
if (activityRunning) {
count.setText(String.valueOf(event.values[0])); **error
}
}
@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 :(得分:0)
您没有计数
的成员变量private SensorManager sensorManager;
private TextView textView;
boolean activityRunning;
private Button buttonReturn;
是片段类
中成员变量的示例count = (TextView) getActivity().findViewById(count);
将TextView设置为未知参考,请正确初始化,即
TextView count = (TextView) getActivity().findViewById(count);
作为职能成员或:
private TextView count;
使用其他类成员变量
然后你需要打电话
sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE)
因为片段本身没有引用getSystemService方法,所以它需要经历它的持有活动
答案 1 :(得分:0)
我也知道 你应该改变
count = (TextView) getActivity().findViewById(count);
到
count = (TextView)getView().findViewById(R.id.count);
因为你必须定义 view ,不要调用 Activity 。