0

가있는 LinearLayout을 포함 내있는 CustomView ID가 포함되어 있지 않습니다생성 FragmentBinding는 텍스트 뷰와있는 CustomView를 포함 내 안드로이드 프로젝트 내 dashboard_fragment_layout.xml에서 데이터 바인딩을 사용하고

<layout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.example.ui.dashboard.DashboardFragment"> 
    <data> 
     <variable 
      name="ViewModel" 
      type="com.example.ui.dashboard.DashboardViewModel" /> 
    </data> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:id="@+id/tvSome" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
     <view 
      android:id="@+id/viewCustom" 
      class="com.example.ui.dashboard.CustomView" 
      android:layout_width="300dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="bottom|center_horizontal" /> 
    </LinearLayout> 
</layout> 

내가 생성을 통해 내 사용자 정의보기에 액세스하려고 FragmentDashboardBinding :

mCustomView = mBinding.viewCustom; 

AndroidStudio에서 'symbol viewCustom을 (를) 해결할 수 없습니다'라는 메시지가 표시됩니다. 그리고 내가 텍스트 뷰이 문제가 없습니다, 그것은 mBinding 객체에서 액세스 할 수있다 : 난 항상 사용자 정의보기에이 오류를 받고 있어요

mSomeTextView = mBinding.tvSome; // all fine, no errors 

, 내 사용자 지정보기 개체에 액세스 할 수있는 유일한 방법은이 작업을 수행하는 것입니다 findViewById를 옛날 방식 : 모두 함께

mCustomView = view.findViewById(R.id.viewCustom); // that works 

:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     mViewDataBinding = DataBindingUtil.inflate(inflater, fragment_dashboard_layout, container, false); 
     view = mViewDataBinding.getRoot(); 

     mCustomView = mViewDataBinding.viewCustom; // 'Can't resolve symbol viewCustom 
     mTextView = mViewDataBinding.tvSome; // all fine 
     mCustomView = view.findViewById(R.id.viewCustom); // that works 
    } 

가 어떻게 생성 된 데이터 바인딩 객체를 통해 사용자 정의보기를 액세스 할 수 있습니까?

답변

1

xml 코드로 com.example.ui.dashboard.CustomView으로 변경하십시오.

그리고 XML 코드에서 클래스를 제거하십시오.

시도해보십시오.

<com.example.ui.dashboard.CustomView 
    android:id="@+id/viewCustom" 
    android:layout_width="300dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="bottom|center_horizontal" /> 
+0

감사합니다. –

+0

행운을 빈다. 내 대답을 받아 들일 수 있습니다. 감사합니다. @ VladMorzhanov – KeLiuyue

0

잘못된보기 태그를 사용했습니다. view 태그를 View에 대체하십시오. 보기 코드를 다음으로 바꾸십시오.

<View 
     android:id="@+id/viewCustom" 
     class="com.example.ui.dashboard.CustomView" 
     android:layout_width="300dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="bottom|center_horizontal" /> 

해피 코딩 !!