在Xamarin中,如果我Inflate
一个View
,如何在TextView
中设置View
的文字?
我想编写一个应用程序,我可以调用不同片段中的方法来在片段之间传递值并显示这些值。
这是我的MainActivity
代码:
public class MainActivity : FragmentActivity
{
string message;
FragmentOrange fragOrange;
FragmentGreen fragGreen;
FragmentRed fragRed;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.activity_main);
fragOrange = new FragmentOrange ();
fragGreen = new FragmentGreen ();
fragRed = new FragmentRed ();
List<Android.Support.V4.App.Fragment> fragments = new List<Android.Support.V4.App.Fragment>();
fragments.Add(fragOrange);
fragments.Add(fragGreen);
fragments.Add(fragRed);
MyPagerAdapter pageAdapter = new MyPagerAdapter(SupportFragmentManager, fragments);
var pager = FindViewById<ViewPager>(Resource.Id.myViewPager);
pager.Adapter = pageAdapter;
pager.SetCurrentItem (0, true);
fragOrange.SetMessage ("Fragment orange");
}
}
这是我的FragmentOrange
代码:
public class FragmentOrange : Android.Support.V4.App.Fragment
{
string message;
TextView textViewDisplay;
View view;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.Inflate(Resource.Layout.fragment_orange, container, false);
textViewDisplay = view.FindViewById<TextView>(Resource.Id.textViewDisplay);
return view;
}
public void SetMessage(string value)
{
message = value;
textViewDisplay.Text = value;
}
}
当我调用SetMessage
函数时,出现以下错误:
对象引用未设置为对象的实例
在这行代码中:
textViewDisplay.Text = value;
这是我的Layout
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Text"
android:id="@+id/textViewDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
提前致谢
答案 0 :(得分:0)
var textView = view.FindViewById<TextView>(Resource.Id.textViewDisplay);
应该是
TextView textview = (TextView) findViewById(R.id.textViewDisplay);
请注意,您无法在没有处理程序的活动中更新textview,因为它是UI的一部分。
//before oncreate
private int i;
Handler handler;
//oncreate
i = 0;
handler = new Handler();
handler.post(mUpdate);
//before the last closing bracket in your activity
Runnable mUpdate = new Runnable(){
public void run() {
textview.setText("your string here");
i++;
handler.postDelayed(this, 1000);
}
};
答案 1 :(得分:0)
TextView textView = view.FindViewById<TextView>(Resource.Id.textViewDisplay);
在给视图充气后添加以下这一行。我的意思是OncreateView
。
答案 2 :(得分:0)
更改此行
var textView = view.FindViewById<TextView>(Resource.Id.textViewDisplay);
到此:
TextView textView = (TextView)view.findViewById(R.id.textViewDisplay);
你应该像这样定义和膨胀你的观点:
LinearLayout view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = (LinearLayout) inflater.inflate(
R.layout.fragment_orange, container, false);
return view;
}
答案 3 :(得分:0)
试试:
var textView = view.FindViewById<TextView>(Resource.Id.textViewDisplay);
更改为:
TextView textview = (TextView) view.findViewById(R.id.textViewDisplay);
然后
view.text.setText(message);