Android tableRow和Fragment

时间:2014-06-16 17:08:50

标签: android tablelayout

我试图让tableLayout使用SectionFragment。

XML:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainMenu" >

    <!--
    This title strip will display the currently visible page title, as well as the page
    titles for adjacent pages.
    -->

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />

    <TableLayout
        android:id="@+id/soundTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        </TableLayout>

</android.support.v4.view.ViewPager>

代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main_menu_dummy,container, false);
    //ON VIENT COLLER LES ELEMENTS ICI
    TableLayout tl = (TableLayout)rootView.findViewById(R.id.soundTable);
    for (int i = 0 ; i <= 1 ; i++) {
        TableRow tr = new TableRow(getActivity());
        tr.setId(100+i) ;
        tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        TextView test = new TextView(getActivity()) ;
        test.setId(200+i);
        test.setText("ahahaha" + i) ;
        tr.addView(test);

        TextView testy = new TextView(getActivity()) ;
        testy.setId(300+i);
        testy.setText("HUHEU" + i) ;
        tr.addView(testy);

        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)) ;
    }

    return rootView;
}

到达

时应用程序崩溃
 tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)) ;

编辑:添加logCat

06-16 19:42:06.704: E/AndroidRuntime(19082): java.lang.NullPointerException
06-16 19:42:06.704: E/AndroidRuntime(19082):    at com.example.beatporttop10.MainMenu$DummySectionFragment.onCreateView(MainMenu.java:143)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.View.measure(View.java:16504)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.View.measure(View.java:16504)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.View.measure(View.java:16504)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2552)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.View.measure(View.java:16504)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.Choreographer.doFrame(Choreographer.java:544)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.os.Handler.handleCallback(Handler.java:733)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.os.Looper.loop(Looper.java:136)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at android.app.ActivityThread.main(ActivityThread.java:5081)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at java.lang.reflect.Method.invokeNative(Native Method)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at java.lang.reflect.Method.invoke(Method.java:515)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-16 19:42:06.704: E/AndroidRuntime(19082):    at dalvik.system.NativeStart.main(Native Method)

我仍然不明白为什么会出现空指针异常。一切都应该没问题。我指的是空视图ID吗?

0 个答案:

没有答案