从片段中获取TableLayout

时间:2014-02-13 13:23:52

标签: android android-fragments classcastexception android-tablelayout

我创建了一个片段,其UI初始化为如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TableLayout android:id="@+id/table_other"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             >
....<-- The rows are dynamically added although it is initialized with the first row already inserted-->
</TableLayout>
</RelativeLayout>

我正在尝试获取TableLayout以获取可以使用以下内容动态添加的行:

tl=(TableLayout) ((RelativeLayout)f.getView()).getChildAt(0);

这会引发错误:

 AndroidRuntime(1959): FATAL EXCEPTION: main
 AndroidRuntime(1959): java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to android.widget.RelativeLayout
AndroidRuntime(1959):   at com.example.newcontactlayout.MainActivity.getTableLayoutFromFragment(MainActivity.java:283)
AndroidRuntime(1959):   at com.example.newcontactlayout.MainActivity.saveContact(MainActivity.java:373)
AndroidRuntime(1959):   at com.example.newcontactlayout.MainActivity.onOptionsItemSelected(MainActivity.java:83)
AndroidRuntime(1959):   at android.app.Activity.onMenuItemSelected(Activity.java:2566)
AndroidRuntime(1959):   at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)

如何从Fragment中获取TableLayout(使用XML添加了Fragment)?

0 个答案:

没有答案