我正在编写一个应用程序来使用Android磨损设备读取心率和步数。心率传感器工作正常,但步数导致问题。但是,在评论Step Counter的监听器并在onResume()中注册null时,该应用程序可与心率传感器配合使用。我没有得到任何关于此的日志,否则我会在这里发布。这是我使用
的代码import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.widget.TextView;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.Wearable;
import java.nio.ByteBuffer;
import java.util.List;
public class MainActivity extends Activity {
private SensorManager mSensorManager;
private TextView mTextViewHeart, mTextViewStep;
private Sensor mHeartRateSensor, mStepCounterSensor;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTextViewHeart = (TextView) stub.findViewById(R.id.value_heart);
mTextViewStep = (TextView) stub.findViewById(R.id.value_step);
}
});
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
mStepCounterSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).build();
mGoogleApiClient.connect();
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(heartListener, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(stepListener, mStepCounterSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(heartListener, mHeartRateSensor);
}
SensorEventListener heartListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
if (event.values.length > 0) {
if (event.values[0] > 0.0f) {
mTextViewHeart.setBackgroundResource(android.R.color.holo_green_light);
mTextViewHeart.setText(Float.toString(event.values[0]));
sendToHandheld(Math.round(event.values[0]), Sensor.TYPE_HEART_RATE);
}
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
SensorEventListener stepListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
if (event.values.length > 0) {
mTextViewStep.setBackgroundResource(android.R.color.holo_green_light);
mTextViewStep.setText(Float.toString(event.values[0]));
sendToHandheld(Math.round(event.values[0]), Sensor.TYPE_STEP_COUNTER);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
private void sendToHandheld(final int val, final int type) {
final PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient);
nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult result) {
final List<Node> nodes = result.getNodes();
if (nodes != null) {
for (int i = 0; i < nodes.size(); i++) {
final Node node = nodes.get(i);
Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), "/" + type, ByteBuffer.allocate(4).putInt(val).array());
Log.d("Sending", type + ":" + val);
}
}
}
});
}
}
更新
以某种方式获取日志
06-15 11:46:02.342 3355-3355/com.hsc.fit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.hsc.fit, PID: 3355
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setBackgroundResource(int)' on a null object reference
at com.hsc.fit.MainActivity$3.onSensorChanged(MainActivity.java:92)
at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:405)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:0)
修正了它。在WatchViewStub
之后调用setOnLayoutInflatedListener()
的{{1}},我的传感器在onResume()
中注册。由于我的文本视图在传感器的监听器中使用,并且尚未分配参考,因此我得到了onResume()
。我在NullPointerException
的监听器中移动了传感器注册,现在可以正常工作了。