【发布时间】:2016-02-21 15:11:21
【问题描述】:
我有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
class="com.xamarin.recipes.filepicker.FileListFragment"
android:id="@+id/FileListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
之后我尝试在 Activity 中访问 FileListFragment:
var fileListFragment = SupportFragmentManager.FindFragmentById(Resource.Id.FileListFragment);
但我在 Watches for fileListFragment 中得到“未知标识符:fileListFragment”
更新。
活动代码:
using Android.Support.V4.App;
using Android.OS;
namespace com.xamarin.recipes.filepicker
{
[Android.App.Activity(Label = "Choose file", MainLauncher = true, Icon = "@drawable/icon")]
public class FilePickerActivity : FragmentActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.ChooseAudio);
var fileListFragment = (FileListFragment)SupportFragmentManager.FindFragmentById(Resource.Id.FileListFragment);
}
}
}
【问题讨论】:
-
为什么你在java中使用
var? Javascript 是另一种语言 :) 试试:getSupportFragmentManager().findFragmentById如果你使用支持库。 -
@Hardy 这是 Xamarin... 不是 Javascript!
-
你能试试这个吗:
FileListFragment fragment = (FileListFragment)SupportFragmentManager.FindFragmentById(Resource.Id.FileListFragment); -
@ManishMulimani,试过了 - 没有帮助