Android Wear append to textView 崩溃应用

Android Wear append to textView crashes app

所以我在尝试检索我的应用程序中的传感器列表时遇到了一个奇怪的问题。

打印到日志工作正常。

当我尝试附加到 textView 时,它崩溃了。

这是我尝试附加的地方:

protected void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.activity_main);

    sensorTextView = (TextView) findViewById(R.id.sensorList);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    initialiseAccelerometer();

    sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
    List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
    for (Sensor sensor : sensors) {
        Log.d("Sensors", "" + sensor.getName());
        sensorTextView.append(sensor.getName());
    }

    sensorManager.registerListener(
                               this,
                               sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                               SensorManager.SENSOR_DELAY_NORMAL);
    mGestureDetector = new GestureDetectorCompat(this, new LongPressListener());
}

随后的错误:

java.lang.RuntimeException: Unable to start activity     
ComponentInfo{com.example.XXXXXX.XXXXXXXXXXX.MainActivity}: 
java.lang.NullPointerException: 
Attempt to invoke virtual method 
'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference

以及具有正确 ID 的 XML 文件:

android:id="@+id/sensorList"

任何人都可以指出我正确的方向或告诉我我做错了什么吗?

activity_main.xml:

<android.support.wearable.view.WatchViewStub
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/rect_activity_main"
app:roundLayout="@layout/round_activity_main"
tools:context=".MainActivity"
tools:deviceIds="wear"></android.support.wearable.view.WatchViewStub>

谢谢, 艾美特

This section of the documentation notes some quirks of WatchViewStub。特别要注意以下几点:

The layouts that you specify for square or round screens are not inflated until WatchViewStub detects the shape of the screen, so your app cannot access their views immediately. To access these views, set a listener in your activity to be notified when the shape-specific layout has been inflated

您可能试图过早设置 TextView 引用,这就是为什么在您尝试附加文本时它为空的原因。如果您像下面的示例代码(也来自文档)那样使用 OnLayoutInflatedListener 来等待适当的 round/square 布局被膨胀,您应该会发现引用已针对两种屏幕类型正确初始化:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wear);

    WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override public void onLayoutInflated(WatchViewStub stub) {
            // Now you can access your views
            TextView tv = (TextView) stub.findViewById(R.id.text);
            ...
        }
    });
}

[注意,我的一位同事指出,目前存在一个错误,要求您为此按特定顺序定义方形布局xml属性和圆形布局xml属性工作 - 我不记得需要先做什么,如果你 运行 遇到问题,请提醒一下。