引用项目中的CustomComponent FindViewById返回null

时间:2018-04-29 11:55:22

标签: xamarin.android android-linearlayout custom-component

在C#Xamarin Android项目中,我正在尝试创建一个自定义复合组件来选择一个月。这扩展了LinearLayout

我希望重新使用它(并保持我的主项目更整洁),所以我有2个项目 - 我的主/父项目,包含我的Activity和多个片段;和我的组件的简单Android项目。

当我使用自己的Activity直接运行组件项目时,它按预期工作,但是当我从主项目引用它时,我的FindViewById返回null

我可以看到thiscontext来自主项目 - 我想了解我做错了什么或我需要添加什么以便它找到组件& #39;布局和控制来膨胀?

主视图(Ui.DateScrollPicker.Droid是我的组件命名空间,ScrollMonthPicker是我的类:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

  .......

  <ui.datescrollpicker.droid.ScrollMonthPicker
    android:id="@+id/my_scroll_month_picker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

引用Android.Support.V4.App.Fragment方法中的override View OnCreateView

    scrollMonthPicker = view.FindViewById<ScrollMonthPicker>(Resource.Id.scroll_month_picker);
    scrollMonthPicker.DateChanged += DatePicker_Changed;

通过扩展LinearLayout,我正在实现2个构造函数,包括调用的ScrollMonthPicker(Context context, IAttributeSet attrs) : base(context, attrs, 0),然后用来扩展我的组件的布局。

组件视图(scroll_month_layout.axml):

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

  <TextView
    android:id="@+id/month_current_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Month" />

  <TextView
    android:id="@+id/year_current_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Year" />

  <android.support.v7.widget.RecyclerView
    android:id="@+id/month_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</merge>

ScrollMonthPicker构造函数:

public ScrollMonthPicker(Context context, IAttributeSet attrs)
    : base(context, attrs, 0)
{
    InflateLayout(context, attrs);
}

ScrollMonthPicker膨胀视图:

private void InflateLayout(Context context, IAttributeSet attrs)
{
    var inflater = LayoutInflater.FromContext(this.Context);
    inflater.Inflate(Resource.Layout.scroll_month_layout, this);

    monthCurrentText = FindViewById<TextView>(Resource.Id.month_current_text);
    yearCurrentText = FindViewById<TextView>(Resource.Id.year_current_text);
    monthRecyclerView = FindViewById<RecyclerView>(Resource.Id.month_recycler_view);
    monthRecyclerView.HasFixedSize = true;

    ......
}

当组件从其自己的&#39;中调用时,该组件工作正常。活动,但是当主项目中的片段引用时,所有FindViewById都为空(monthCurrentTextyearCurrentTextmonthRecyclerView)。

如果我等待从monthRecyclerView.HasFixedSize的主片段中抛出异常,我不会得到一个空引用异常,但是这个:

Unhandled Exception:

    System.NotSupportedException: Could not activate JNI Handle 0x7fff30d40b00 (key_handle 0x8f4c53e) of Java type 'md53fd9f663a5e8ddcd0a5da1b57d05fd99/ScrollMonthPicker' as managed type 'Ui.DateScrollPicker.Droid.ScrollMonthPicker'.

如何通过引用单独的项目使其按预期填充?

非常感谢。

1 个答案:

答案 0 :(得分:1)

看来你们两个项目都是android项目。你不能在一个项目中使用另一个android项目代码。我认为您需要将CustomComponent项目创建为库。