Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

java - 找不到ID为该字段的必需视图,但ID存在(required view with id for field was not found but id exists)

I have a little problem.

(我有一点问题。)

I'm trying to BindView with the id ongoing_call_layout .

(我正在尝试将ID为ID的BindView ongoing_call_layout 。)

I have that id, it exists and its a ConstraintLayout object in xml.

(我有该ID,它存在并且在xml中有一个ConstraintLayout对象。)

When running the program it acts like the id "ongoing_call_layout" doesn't exist.

(运行该程序时,它的行为就像id“ ongoing_call_layout”不存在。)

I've tried cleaning and rebuilding the project and also using findViewById instead of BindView but still, it says that the id doesn't exist.

(我试过清理和重建项目,并使用findViewById代替BindView但仍然说该ID不存在。)

In the code itself its bolded (AndroidStudio do know where the id is).

(在代码本身中,其粗体(AndroidStudio知道ID在哪里)。)

Below is the related code

(下面是相关代码)

Here is my OngoingCallActivity.java (where the error comes from):

(这是我的OngoingCallActivity.java (错误来自哪里):)

@BindView(R.id.ongoing_call_layout) ConstraintLayout mOngoingCallLayout;
.
.
.
 ButterKnife.bind(this);
.
.

The error:

(错误:)

Unable to start activity ComponentInfo{com.chooloo.www.callmanager/com.chooloo.www.callmanager.ui.activity.OngoingCallActivity}: java.lang.IllegalStateException: Required view 'ongoing_call_layout' with ID 2131362055 for field 'mOngoingCallLayout' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.

Here is my on_going_call.xml (where the id comes from):

(这是我的on_going_call.xml (ID来自何处):)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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/ongoing_call_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="ltr"
    tools:context=".ui.activity.OngoingCallActivity">

    <!-- Call Timer -->
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guide_status_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="24dp" />

    <TextView
        android:id="@+id/text_stopwatch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guide_status_bar"
        tools:text="00:00" />

    <!-- Caller Number (Temporary) -->

    <!-- Caller Pic -->

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="129dp"
        android:layout_height="135dp"
        app:layout_constraintBottom_toTopOf="@+id/text_caller"
        app:layout_constraintEnd_toEndOf="@+id/text_caller"
        app:layout_constraintStart_toStartOf="@+id/text_caller"
        app:layout_constraintTop_toBottomOf="@+id/text_stopwatch">

        <ImageView
            android:id="@+id/image_placeholder"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tint="@color/selector_button_icon"
            app:srcCompat="@drawable/ic_account_circle_black_24dp" />

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/image_photo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            app:civ_border_width="0dp" />

    </FrameLayout>

    <!-- Caller Name -->
    <TextView
        android:id="@+id/text_caller"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10sp"
        android:paddingRight="10sp"
        android:textAlignment="center"
        android:textAppearance="@style/TextAppearance.MaterialComponents.Headline4"
        android:textColor="?attr/iconColor"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/text_status"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        tools:text="Jesus" />

    <!-- Call Status -->
    <TextView
        android:id="@+id/text_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle1"
        android:textColor="?attr/colorAccent"
        app:layout_constraintBottom_toTopOf="@+id/guide_eye_level"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="@+id/text_caller"
        tools:text="@string/status_call_incoming" />

    <!-- Hold Button -->
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guide_eye_level"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.44" />

    <ImageView
        android:id="@+id/button_hold"
        android:layout_width="@dimen/flat_button_size"
        android:layout_height="62dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="120dp"
        android:layout_marginEnd="16dp"
        android:layout_weight="1"
        android:background="@drawable/ripple_oval"
        android:clickable="true"
        android:focusable="true"
        android:padding="@dimen/flat_button_padding"
        android:tint="@color/selector_button_icon"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/button_add_call"
        app:layout_constraintEnd_toStartOf="@+id/button_speaker"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button_mute"
        app:layout_constraintTop_toTopOf="@+id/guide_eye_level"
        app:layout_constraintVertical_bias="0.32999998"
        app:layout_constraintVertical_chainStyle="packed"
        app:srcCompat="@drawable/ic_pause_black_24dp"
        tools:visibility="visible" />

    <!-- Mute Button -->
    <ImageView
        android:id="@+id/button_mute"
        android:layout_width="@dimen/flat_button_size"
        android:layout_height="@dimen/flat_button_size"
        android:layout_marginEnd="16dp"
        android:layout_weight="1"
        android:background="@drawable/ripple_oval"
        android:clickable="true"
        android:focusable="true"
        android:padding="@dimen/flat_button_padding"
        android:tint="@color/selector_button_icon"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="@+id/button_hold"
        app:layout_constraintEnd_toStartOf="@+id/button_hold"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button_hold"
        app:srcCompat="@drawable/ic_mic_off_black_24dp"
        tools:visibility="visible" />

    <!-- Speaker Button -->
    <ImageView
        android:id="@+id/button_speaker"
        android:layout_width="@dimen/flat_button_size"
        android:layout_height="@dimen/flat_button_size"
        android:layout_marginStart="16dp"
        android:layout_weight="1"
        android:background="@drawable/ripple_oval"
        android:clickable="true"
        android:focusable="true"
        android:padding="@dimen/flat_button_padding"
        android:tint="@color/selector_button_icon"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="@+id/button_hold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button_hold"
        app:layout_constraintTop_toTopOf="@+id/button_hold"
        app:srcCompat="@drawable/ic_volume_up_black_24dp"
        tools:visibility="visible" />

    <!-- Keypad Button -->
    <ImageView
        android:id="@+id/button_keypad"
        android:layout_width="@dimen/flat_button_size"
        android:layout_height="@dimen/flat_button_size"
        android:layout_marginEnd="16dp"
        android:layout_weight="1"
        android:background="@drawable/ripple_oval"
        android:clickable="true"
        android:focusable="true"
        android:padding="@dimen/flat_button_padding"
        android:tint="@color/selector_button_icon"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="@+id/button_add_call"
        app:layout_constraintEnd_toStartOf="@+id/button_add_call"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button_add_call"
        app:srcCompat="@drawable/ic_dialpad_black_24dp"
        tools:visibility="visible" />

    <!-- Add Call Button -->
    <ImageView
        android:id="@+id/button_add_call"
        android:layout_width="65dp"
        android:layout_height="67dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:background="@drawable/ripple_oval"
        android:clickable="true"
        android:focusable="true"
        android:padding="@dimen/flat_button_padding"
        android:tint="@color/selector_button_icon"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button_keypad"
        app:layout_constraintTop_toBottomOf="@+id/button_hold"
        app:srcCompat="@drawable/ic_add_black_24dp"
        tools:visibility="visible" />

    <!-- Answer Button -->
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/answer_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginBottom="32dp"
        android:scaleType="center"
        android:src="@drawable/ic_call_black_24dp"
        android:tint="@color/green_phone"
        app:backgroundTint="@color/white"
        app:fabCustomSize="72dp"
        app:fabSize="normal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:maxImageSize="36dp" />

    <!-- Reject Button -->
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/reject_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="32dp"
        android:scaleType="center"
        android:src="@drawable/ic_call_end_black_24dp"
        android:tint="@colo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I am not sure how it works for butterknife, however, to enable data binding in your application you need to declare the following in the android section of your AndroidManifest.xml .

(我不确定它如何适用于黄油刀,但是,要在应用程序中启用数据绑定,您需要在AndroidManifest.xmlandroid部分中声明以下内容。)

android {
    dataBinding {
        enabled = true
    }
}

And then you need to wrap your on_going_call.xml.xml with layout as follows.

(然后,您需要按如下所示将on_going_call.xml.xmllayout 。)

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    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">

    <androidx.constraintlayout.widget.ConstraintLayout 
        android:id="@+id/ongoing_call_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutDirection="ltr">

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

And yes, as stated in the comment, you need to setContentView in your activity.

(是的,如评论中所述,您需要在活动中设置setContentView 。)

I hope that helps!

(希望对您有所帮助!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...