0

我知道这个问题已经在这里回答了很多次,但我似乎仍然无法解决我的问题。我正在尝试创建一个读取用户心率并将该信息发送到手机的应用程序。我的问题是我已经实现了一个WatchViewStub,但是每次我运行程序时它都会给我一个NullPointerException on a null object reference. 我真的无法弄清楚为什么这给了我这样的错误。有人可以解释我做错了什么吗?先感谢您。

错误来自第 31 行,即:

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>

来自 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)
4

0 回答 0