Android 当我将wear布局从BoxInsetLayout更改为WatchViewStub时,WearableListView始终为空

Android 当我将wear布局从BoxInsetLayout更改为WatchViewStub时,WearableListView始终为空,android,android-layout,wear-os,Android,Android Layout,Wear Os,我有一个应用程序使用BoxInsetLayout,但圆形布局不起作用,所以我必须将其更改为WatchViewStub 我改为使用圆形布局或方形布局的代码,这样可以工作。原始布局文件中有一个WearableListView,它只是一个文件。现在是3个文件,主文件和圆形或方形文件。现在,listview始终为空,即使没有编译时错误 我的onCreate在主活动中 @Override protected void onCreate(Bundle savedInstanceState) { su

我有一个应用程序使用BoxInsetLayout,但圆形布局不起作用,所以我必须将其更改为WatchViewStub

我改为使用圆形布局或方形布局的代码,这样可以工作。原始布局文件中有一个WearableListView,它只是一个文件。现在是3个文件,主文件和圆形或方形文件。现在,listview始终为空,即使没有编译时错误

我的onCreate在主活动中

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

    WatchViewStub stub = (WatchViewStub) findViewById(R.id.stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub watchViewStub) {
            mRectangleBackground = (RelativeLayout) findViewById(R.id.rectLayout);
            mRoundBackground = (RelativeLayout) findViewById(R.id.roundLayout);

        }
    });

    WearableListView listView = (WearableListView) findViewById(R.id.list); // this is always null

    listView.setHasFixedSize(true);
    listView.setAdapter(new SportAdapter(this));
    listView.setClickListener(this);
}
我的视图选择xml文件(WatchViewStub)


所以我通过添加另一个方法loadAdapter修复了它,如下所示

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


    WatchViewStub stub = (WatchViewStub) findViewById(R.id.stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub watchViewStub) {
            mRectangleBackground = (RelativeLayout) findViewById(R.id.rectLayout);
            mRoundBackground = (RelativeLayout) findViewById(R.id.roundLayout);
            listView = (WearableListView) findViewById(R.id.list);
            loadAdapter();
        }
    });

}

private void loadAdapter() {
    listView.setHasFixedSize(true);
    listView.setAdapter(new SportAdapter(this));
    listView.setClickListener(this);
}
如本例所示


呃…

出于好奇,你为什么要从BoxInsetLayout切换?@dazza5000,因为方形手表应用程序中的布局无法很好地转换为圆形手表
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
app:layout_box="all"
android:id="@+id/rectLayout">

<TextView
    android:id="@+id/text_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:fontFamily="sans-serif-thin"
    android:paddingBottom="5dp"
    android:text="@string/text_select_sport"
    android:textColor="@color/white"
    android:textSize="26sp" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text_title"
    android:background="@color/white"
    android:padding="4dp">

    <android.support.wearable.view.WearableListView
        android:layout_centerInParent="true"
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:dividerHeight="0dp"/>

</RelativeLayout>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:id="@+id/parent"
android:background="@color/green_forest">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    app:layout_box="all">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text_title"
        android:text="@string/text_select_sport"
        android:textSize="26sp"
        android:textColor="@color/white"
        android:fontFamily="sans-serif-thin"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:paddingBottom="5dp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:padding="4dp"
        android:layout_below="@+id/text_title">

        <android.support.wearable.view.WearableListView
            android:layout_centerInParent="true"
            android:id="@+id/list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:dividerHeight="0dp"/>

    </RelativeLayout>

</RelativeLayout>

</android.support.wearable.view.BoxInsetLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_select);
    WearableListView listView = (WearableListView) findViewById(R.id.list);

    listView.setHasFixedSize(true);
    listView.setAdapter(new SportAdapter(this));
    listView.setClickListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_select);


    WatchViewStub stub = (WatchViewStub) findViewById(R.id.stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub watchViewStub) {
            mRectangleBackground = (RelativeLayout) findViewById(R.id.rectLayout);
            mRoundBackground = (RelativeLayout) findViewById(R.id.roundLayout);
            listView = (WearableListView) findViewById(R.id.list);
            loadAdapter();
        }
    });

}

private void loadAdapter() {
    listView.setHasFixedSize(true);
    listView.setAdapter(new SportAdapter(this));
    listView.setClickListener(this);
}