I know this question has been answered many times on here but I still can't seem to fix my problem. I am trying to create an application that reads the users heart rate and sends that information to the phone. My problem is that I have implemented a WatchViewStub
but every time I run the program it keeps giving me a NullPointerException
on a null object reference
. I really can't seem to figure out why this is giving me such error. Could someone please explain what I am doing wrong? Thank you in advance.
Error is coming from line 31 which is:
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
HeartRateWearActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.round_activity_heart_rate);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
timeStampTxt = (TextView) stub.findViewById(R.id.timeStampTxt);
hrTxt = (TextView) stub.findViewById(R.id.hrTxt);
measureBtn = (Button) stub.findViewById(R.id.measureBtn);
measureBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!toggle) {
startService(new Intent(getApplicationContext(), SensorService.class));
toggle = true;
} else {
stopService(new Intent(getApplicationContext(), SensorService.class));
toggle = false;
}
}
});
}
});
this.registerReceiver(mMessageReceiver, new IntentFilter("com.example.Broadcast"));
}
round_activity_heart_rate.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".HeartRateWearActivity"
tools:deviceIds="wear_round"
android:id="@+id/r_bg"
android:background="#ED2D50">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hrTxt"
android:textSize="80dp"
android:text="0"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bpm"
android:id="@+id/textView2"
android:textColor="@color/white"
android:layout_alignBaseline="@+id/hrTxt"
android:layout_alignBottom="@+id/hrTxt"
android:layout_toEndOf="@+id/hrTxt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="last update"
android:id="@+id/textView3"
android:textSize="10dp"
android:layout_below="@+id/hrTxt"
android:layout_centerHorizontal="true"
android:textColor="@color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="7:15"
android:id="@+id/timeStampTxt"
android:textSize="12dp"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true"
android:textColor="@color/white" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/measureBtn"
android:layout_below="@+id/timeStampTxt"
android:layout_centerHorizontal="true"
android:checked="false" />
</RelativeLayout>
Error from Logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.health/com.example.user.health.HeartRateWearActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.wearable.view.WatchViewStub.setOnLayoutInflatedListener(android.support.wearable.view.WatchViewStub$OnLayoutInflatedListener)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.wearable.view.WatchViewStub.setOnLayoutInflatedListener(android.support.wearable.view.WatchViewStub$OnLayoutInflatedListener)' on a null object reference
at com.example.user.health.HeartRateWearActivity.onCreate(HeartRateWearActivity.java:31)