尝试在Xamarin上创建片段时获取Android.Content.Res.Resources + NotFoundException

时间:2014-09-30 06:50:56

标签: c# android android-fragments xamarin xamarin.android

我正在尝试使用片段创建Tabs

这是我活动的代码:

        #region Send Data To Fragment
        int productID = Intent.GetIntExtra("ProductID", -1);
        Bundle detailsBundle = new Bundle();
        detailsBundle.PutInt("ID", productID);
        #endregion

        #region Tabs
        ActionBar.Tab tab = ActionBar.NewTab();
        tab.SetText("Details");
        tab.TabSelected += (sender, args) => {
            var detailsFragment = new DetailsFragment();
            detailsFragment.Arguments = detailsBundle;
            var ft = FragmentManager.BeginTransaction();
            ft.Add(Resource.Id.fragmentConteiner, detailsFragment);
            ft.Commit();
        };
        ActionBar.AddTab(tab);
        //Here Comes another Tab...

我不会忘记ActionBar.NavigationMode = ActionBarNavigationMode.Tabs

之前的SetContentView

这是我的片段类:

public class DetailsFragment : Fragment {
    TextView _name;
    TextView _manufacturer;
    TextView _price;
    ImageView _image;
    Product _product;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.Inflate( PhoneCatalog.Resource.Id.fragmentConteiner, container,false); //this is where Exception is thrown saying "Resource ID #0x7f070009 type #0x12 is not valid"

        _name = view.FindViewById<TextView>(Resource.Id.detailsTextName);
        _manufacturer = view.FindViewById<TextView>(Resource.Id.detailsTextManufacturer);
        _price = view.FindViewById<TextView>(Resource.Id.detailsTextPrice);
        _product = ProductData.Service.GetProducts(this.Arguments.GetInt("ID"));

        UpdateUI();
        return view;
    }

    private void UpdateUI() {
        _name.Text = _product.Name;
        _manufacturer.Text = _product.Manufacturer;
        _price.Text = Convert.ToString(_product.Price);
    }
}

这是我在OnCreateView参数中得到的结果 http://i.imgur.com/pp6ySHn.png

这是我的布局xml文件,我有FrameLayout

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:p1="http://schemas.android.com/apk/res/android"
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:layout_width="match_parent"
    p1:layout_height="match_parent"
    p1:id="@+id/detailsScrollView">
    <FrameLayout
        p1:minWidth="25px"
        p1:minHeight="25px"
        p1:layout_width="match_parent"
        p1:layout_height="match_parent"
        p1:id="@+id/fragmentConteiner">
        <RelativeLayout
            p1:minWidth="25px"
            p1:minHeight="25px"
            p1:layout_width="match_parent"a
            p1:layout_height="match_parent"
            p1:id="@+id/detailsRelativeLayout">
            <TextView
                p1:text="Text"
                p1:layout_width="wrap_content"
                p1:layout_height="wrap_content"
                p1:id="@+id/detailsTextName"
                p1:padding="5dp"
                p1:textSize="22sp"
                p1:textStyle="bold" />
            <ImageView
                p1:src="@android:drawable/ic_menu_gallery"
                p1:layout_width="match_parent"
                p1:layout_height="344.5dp"
                p1:layout_below="@id/detailsTextName"
                p1:id="@+id/detailsImageView" />
            <TextView
                p1:text="Details"
                p1:textAppearance="?android:attr/textAppearanceLarge"
                p1:layout_width="wrap_content"
                p1:layout_height="wrap_content"
                p1:layout_below="@id/detailsImageView"
                p1:id="@+id/detailsTextView"
                p1:textStyle="bold"
                p1:padding="5dp" />
            <TextView
                p1:text="Manufacturer"
                p1:textAppearance="?android:attr/textAppearanceMedium"
                p1:layout_width="wrap_content"
                p1:layout_height="wrap_content"
                p1:layout_below="@id/detailsTextView"
                p1:id="@+id/detailsTextView1"
                p1:padding="5dp" />
            <TextView
                p1:text="Price"
                p1:textAppearance="?android:attr/textAppearanceMedium"
                p1:layout_width="wrap_content"
                p1:layout_height="wrap_content"
                p1:layout_below="@id/detailsTextView1"
                p1:id="@+id/textView1"
                p1:padding="5dp" />
            <TextView
                p1:text="Text"
                p1:layout_width="match_parent"
                p1:layout_height="32.6dp"
                p1:layout_toRightOf="@id/detailsTextView1"
                p1:id="@+id/detailsTextManufacturer"
                p1:layout_below="@id/detailsTextView"
                p1:textSize="20sp"
                p1:padding="5dp" />
            <TextView
                p1:text="Text"
                p1:layout_width="wrap_content"
                p1:layout_height="wrap_content"
                p1:layout_below="@id/detailsTextView1"
                p1:id="@+id/detailsTextPrice"
                p1:layout_toRightOf="@id/textView1"
                p1:padding="5dp"
                p1:textSize="20sp" />
        </RelativeLayout>
    </FrameLayout>
</ScrollView>

任何想法我做错了什么?也许我在错误的地方或其他地方<FrameLayout></FrameLayout>

1 个答案:

答案 0 :(得分:0)

好的,修好了。我想我将错误的参数传递给inflater.Inflate方法

ProductDetails布局xml我有FrameLayour(上面发布)我只留下了这个:

<ScrollView xmlns:p1="http://schemas.android.com/apk/res/android"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="@+id/detailsScrollView">
<FrameLayout
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:layout_width="match_parent"
    p1:layout_height="match_parent"
    p1:id="@+id/fragmentConteiner" />

然后创建了新的布局文件ProductDetailsFragment并在此处移动了其余的xml代码:

<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="@+id/detailsRelativeLayout">
<TextView
    p1:text="Text"
    p1:layout_width="wrap_content"
    p1:layout_height="wrap_content"
    p1:id="@+id/detailsTextName"
    p1:padding="5dp"
    p1:textSize="22sp"
    p1:textStyle="bold" />
<ImageView
    p1:src="@android:drawable/ic_menu_gallery"
    p1:layout_width="match_parent"
    p1:layout_height="344.5dp"
    p1:layout_below="@id/detailsTextName"
    p1:id="@+id/detailsImageView" />
<TextView
    p1:text="Details"
    p1:textAppearance="?android:attr/textAppearanceLarge"
    p1:layout_width="wrap_content"
    p1:layout_height="wrap_content"
    p1:layout_below="@id/detailsImageView"
    p1:id="@+id/detailsTextView"
    p1:textStyle="bold"
    p1:padding="5dp" />
<TextView
    p1:text="Manufacturer"
    p1:textAppearance="?android:attr/textAppearanceMedium"
    p1:layout_width="wrap_content"
    p1:layout_height="wrap_content"
    p1:layout_below="@id/detailsTextView"
    p1:id="@+id/detailsTextView1"
    p1:padding="5dp" />
<TextView
    p1:text="Price"
    p1:textAppearance="?android:attr/textAppearanceMedium"
    p1:layout_width="wrap_content"
    p1:layout_height="wrap_content"
    p1:layout_below="@id/detailsTextView1"
    p1:id="@+id/textView1"
    p1:padding="5dp" />
<TextView
    p1:text="Text"
    p1:layout_width="match_parent"
    p1:layout_height="32.6dp"
    p1:layout_toRightOf="@id/detailsTextView1"
    p1:id="@+id/detailsTextManufacturer"
    p1:layout_below="@id/detailsTextView"
    p1:textSize="20sp"
    p1:padding="5dp" />
<TextView
    p1:text="Text"
    p1:layout_width="wrap_content"
    p1:layout_height="wrap_content"
    p1:layout_below="@id/detailsTextView1"
    p1:id="@+id/detailsTextPrice"
    p1:layout_toRightOf="@id/textView1"
    p1:padding="5dp"
    p1:textSize="20sp" />

DetailsFragment课程中我改变了这个: View view = inflater.Inflate( PhoneCatalog.Resource.Id.fragmentConteiner, container,false); 对此: View view = inflater.Inflate( PhoneCatalog.Resource.Layout.ProductDetailsFragment, container,false);