Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将视图从单独的布局文件添加到水平滚动视图_C#_Android_Xamarin Studio - Fatal编程技术网

C# 将视图从单独的布局文件添加到水平滚动视图

C# 将视图从单独的布局文件添加到水平滚动视图,c#,android,xamarin-studio,C#,Android,Xamarin Studio,我试图将一个卡片列表添加到滚动视图中,使用一个单独的xml文件作为布局 但是,按下按钮时,我出现以下错误: System.NullReferenceException: Object reference not set to an instance of an object 我可以在不使用Card.xml布局的情况下添加文本,当我使用它作为布局时,我会得到错误 protected override void OnCreate(Bundle bundle) { base.OnCreate

我试图将一个卡片列表添加到滚动视图中,使用一个单独的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);


    };
}
protectedoverride void OnCreate(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
Button buttonWhite=FindViewById(Resource.Id.button2);
//在点击按钮时,循环处理一手牌,并使用卡片布局文件将每一手牌添加到ScrollVew中
按钮白色。单击+=委派
{
HorizontalScrollView ScrollView=(HorizontalScrollView)FindViewById(Resource.Id.horizontalScrollView1);
//从Cardlayout文件创建布局
LinearLayout测试卡=(LinearLayout)FindViewById(Resource.Layout.card);
//LoopThrough玩家手拿并使用文本添加到卡片布局,然后将带有文本的布局添加到scrollview
foreach(p.hand中的卡片测试)
{
//创建新的文本视图
TextView Test_Text=新的TextView(此);
//将新文本视图文本值设置为卡片文本
Test_Text.Text=Test.Text;
//将视图添加到布局
测试卡。添加视图(测试文本);
}
//将布局添加到scrollview
ScrollView.AddView(测试卡);
};
}

您应该扩大卡资源布局,它不在您当前的上下文中。因此,您应该替换:

 LinearLayout Test_card = (LinearLayout)FindViewById(Resource.Layout.Card);
使用(Java):

(也许):


getLayoutInflater告诉我它在当前上下文中不存在?此方法(getLayoutInflater)是活动上下文的本机(Java)方法。请参阅此处的文档:以及另一个可能对您有所帮助的答案:
LinearLayout Test_card = (LinearLayout) getLayoutInflater().inflate(Resource.Layout.Card);
LinearLayout Test_card = (LinearLayout) this.LayoutInflater.Inflate(
                Resource.Layout.Card ...);