0

はのLinearLayoutが含まれている私のCustomViewのIDが含まれていません:生成FragmentBindingはのTextViewとの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で 'シンボルビューカスタムを解決できません'が表示されています。そして、私はTextViewにして、この問題を持っていない、それは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" /> 

ハッピーコーディング!

関連する問題