我有一个使用BoxInsetLayout的应用程序,但是圆形布局没有用完,所以我必须将其更改为WatchViewStub。
我改为代码,以便它使用圆形布局或方形布局,并且可以找到。原始布局文件中有一个WearableListView,它只是一个文件。现在它是3个文件,主要文件,圆形或方形文件。现在listview始终为null,即使没有编译时错误。
我在主要活动中的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始终为null?
这是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);
}
答案 0 :(得分:0)
所以我通过添加另一个方法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);
}
如此示例中所示https://gist.github.com/PomepuyN/c30760eaee1e58fdd8fa
啊...