0

I am trying to make a heart rate monitor app on Android wear, and when I try to run app on my device (LG G watch R) I have

"Unfortunately, app has stopped"

and on Android studio I see this error:

My error

package com.example.ryuu.appka;

import android.app.Activity;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.widget.TextView;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class activity_wear extends Activity implements SensorEventListener {

private TextView mTextView;
private static final String TAG = "MainActivity";
private TextView mTextViewStepCount;
private TextView mTextViewStepDetect;
private TextView mTextViewHeart;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_wear);

    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {

        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
            mTextViewStepCount = (TextView) stub.findViewById(R.id.step_count);
            mTextViewStepDetect = (TextView) stub.findViewById(R.id.step_detect);
            mTextViewHeart = (TextView) stub.findViewById(R.id.heart);
            getStepCount();
        }
    });
}

private void getStepCount() {
    SensorManager mSensorManager = ((SensorManager)getSystemService(SENSOR_SERVICE));
    Sensor mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
    Sensor mStepCountSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    Sensor mStepDetectSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);

    mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(this, mStepCountSensor, SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(this, mStepDetectSensor, SensorManager.SENSOR_DELAY_NORMAL);
}

private String currentTimeStr() {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
    return df.format(c.getTime());
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
        String msg = "" + (int)event.values[0];
        mTextViewHeart.setText(msg);
        Log.d(TAG, msg);
    }
    else if (event.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
        String msg = "Count: " + (int)event.values[0];
        mTextViewStepCount.setText(msg);
        Log.d(TAG, msg);
    }
    else if (event.sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
        String msg = "Detected at " + currentTimeStr();
        mTextViewStepDetect.setText(msg);
        Log.d(TAG, msg);
    }
    else
        Log.d(TAG, "Unknown sensor type");
}


@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    Log.d(TAG, "onAccuracyChanged - accuracy: " + accuracy);
}

}

Whole source code is on Google Drive

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Nikola Jan 04 '16 at 01:38
  • Welcome to Stack Overflow. I have fixed English issues with your answer. – Rohit Gupta Jan 06 '16 at 01:35

1 Answers1

0

It seems that your mTextViewHeart is null. Apparently at the time that the onSensorChanged() method is called, the mTextViewHeart is not yet initialized.

You can prevent that by checking if it is not null before trying to invoke a method on it.

Also read What is a Null Pointer Exception, and how do I fix it? to help you understand your problem better.

Nikola
  • 2,093
  • 3
  • 22
  • 43