簡體   English   中英

從單獨的布局文件向水平滾動視圖添加視圖

[英]Add view to Horizontal Scroll View from separate layout file

我正在嘗試使用A單獨的xml文件作為布局將紙牌列表添加到滾動視圖中。

但是在按下按鈕時,出現以下錯誤:

System.NullReferenceException: Object reference not set to an instance of an object

我可以不使用Card.xml布局就可以添加文本,當我將其用作布局時會收到錯誤。

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);



    Button buttonWhite = FindViewById<Button>(Resource.Id.button2);



    //On button click, loop though dealt hand of cards and add each to Scrollveiw using Card layout file
    buttonWhite.Click += delegate
    {

        HorizontalScrollView ScrollView = (HorizontalScrollView)FindViewById(Resource.Id.horizontalScrollView1);

        //create layout from Cardlayout file
        LinearLayout Test_card = (LinearLayout)FindViewById(Resource.Layout.Card);

        //loopthough Players hand and use text to add to card layout then add layout with text to scrollview
        foreach (Card Test in p.hand)
        {


            //create new textview
            TextView Test_Text = new TextView(this);


            //set new textviews text value to card text
            Test_Text.Text = Test.text;

            //addview to layout
            Test_card.AddView(Test_Text);

        }

        //add layout to scrollview
        ScrollView.AddView(Test_card);


    };
}

您應該增加Card資源的布局,它不在您當前的上下文中。 因此,您應該替換:

 LinearLayout Test_card = (LinearLayout)FindViewById(Resource.Layout.Card);

與(Java):

LinearLayout Test_card = (LinearLayout) getLayoutInflater().inflate(Resource.Layout.Card);

C#(也許):

LinearLayout Test_card = (LinearLayout) this.LayoutInflater.Inflate(
                Resource.Layout.Card ...);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM