【发布时间】:2015-12-30 18:35:09
【问题描述】:
我有一个使用 BoxInsetLayout 的应用,但圆形布局无法正常工作,因此我必须将其更改为 WatchViewStub。
我更改了代码,以便它使用圆形布局或方形布局,并且可以找到。原始布局文件中有一个 WearableListView,它只是一个文件。现在它是3个文件,主要的一个,圆形或方形的一个。现在即使没有编译时错误,列表视图也始终为空。
我在主活动中的 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)
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/view_select_square"
app:roundLayout="@layout/view_select_round"
android:keepScreenOn="true"
android:id="@+id/stub"
android:background="@color/green_forest">
</android.support.wearable.view.WatchViewStub>
方形布局
<?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>
每当我调用 listView 之类的 setAdapter、setHasFixedSize 时,都会收到空引用错误。为什么我的 listView 总是为空?
这是BoxInsetLayout时的xml文件
<?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);
}
【问题讨论】:
标签: android android-layout wear-os