视图上的Android数据绑定视图标记不正确:null

问题描述 投票:0回答:1

问题标签可能在stackoverflow中很熟悉,但我在这里有一些情况。我想实现我的应用程序将有一个底部菜单栏,将在整个应用程序中可见。所以我添加了一个视图并将其设置在我的基本活动上,如下所示

@Override
public void setContentView(int layoutResID) {
    fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
    configureToolbar(fullLayout);
    subActivityContent = (FrameLayout) fullLayout.findViewById(R.id.content_frame);
    getLayoutInflater().inflate(layoutResID, subActivityContent, true);
    super.setContentView(fullLayout);
}

以前就像是:

@Override
public void setContentView(int layoutResID) {
    View view = getLayoutInflater().inflate(layoutResID, null);
    configureToolbar(view);
    super.setContentView(view);
}

现在在某些页面上有数据绑定。其名称如下:

   viewModel = new MyViewModel(someId, someName, false, emptyString);
   binding = DataBindingUtil.setContentView(this, R.layout.activity_my_layout);
   binding.setItem(viewModel);

现在我在第二行遇到错误。

这是我的活动布局。第一个是common_base的常见布局,第二个是数据绑定布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">

<include android:id="@+id/app_bar" layout="@layout/app_bar" />

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.roughike.bottombar.BottomBar
    android:id="@+id/bottomBar"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    app:bb_tabXmlResource="@xml/bottombar_tabs" />

第二个布局:

<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/apk/res-auto">

<data>
    <import type="android.view.View" />

    <variable
        name="item"
        type="io.ppp.views.someActivity" />
</data>

<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="io.ppp.views.someActivity">

    <include  android:id="@+id/app_bar"  layout="@layout/app_bar" />
    <!--<include  layout="@layout/activity_base" />-->




    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webViewLoader"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />


    <RelativeLayout
        android:id="@+id/progress_indicator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="false"
        android:layout_centerInParent="true"
        android:visibility="@{item.loading ? View.VISIBLE : View.GONE}">

        <ProgressBar
            android:id="@+id/search_progress"
            style="@style/Base.Widget.AppCompat.ProgressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:indeterminate="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center"
            android:layout_marginTop="50dp"
            android:text="@{item.loadingText}"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

</RelativeLayout>

android android-layout android-databinding
1个回答
1
投票

我不认为这是在您创建活动时在您的应用程序中实现底部导航栏的良好解决方案,并且用户将清楚每次更改活动时屏幕将完全交换(包括底部栏)将固定元素保留在屏幕底部,用户将用它来引导自己穿过应用程序。

在Android中实现底部导航比在iOS中更加棘手,因为您将不得不使用ONE SINGLE Activity和Fragments(如果您有很多屏幕,那么它们很多)可以实现像应用程序一样平滑的底部导航。

我的建议是将您的活动转换为片段,使用底栏和片段容器实现底部导航Activity,然后从那里开始。

从API 25开始,它引入了BottomNavigationView,这将使您的生活更轻松,您不需要使用GitHub的BottomBar库。我也建议看看AHBottomNavigation,在我看来比原生的更完整,给你一个更好的控制和选择你的底栏。

© www.soinside.com 2019 - 2024. All rights reserved.