Android中的表格布局问题

时间:2012-03-25 16:31:40

标签: android android-layout

我想实现表格布局。在那里我想要实现两行,所以我的代码如下,但它显示强制关闭。可以任何一个帮助,我从这五天坚持这个

public  class SongsActivity extends Activity{

    DemoView demoview ;
    DemoView2 finalview;
    DemoView3 dview;
    DemoView4 deview;

    ViewFlipper c ;
    TableLayout d;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.songs_layout);
        demoview = new DemoView(this);
        finalview = new DemoView2(this);
        dview= new DemoView3(this);
        deview = new DemoView4(this);
         d= new TableLayout(this);


        c=( ViewFlipper )findViewById(R.id.viewFlipper1);
        c.addView(demoview,0);
        c.addView(finalview, 1);
        c.addView(dview, 2);
        c.addView(deview, 3);
        c.setAutoStart(true);
        c.setFlipInterval(500);
        c.startFlipping();



        TableRow rw1=new TableRow(this);
         TableRow rw2=new TableRow(this);
         TableRow.LayoutParams params = new TableRow.LayoutParams();  
         params.span = 6;
        rw1.addView(c);
        //rw2.addView();
        d.addView(rw2);
        d.addView(rw1);
        setContentView(d);
    }

我在程序中使用的布局的xml文件如下

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ViewFlipper
            android:id="@+id/viewFlipper1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ViewFlipper>


        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>

    </TableLayout>

我的日志cat文件如下所示,显示错误

03-25

     20:56:35.963: E/AndroidRuntime(369): FATAL EXCEPTION: main
    03-25 21:01:37.913: E/AndroidRuntime(399): FATAL EXCEPTION: main
    03-25 21:01:37.913: E/AndroidRuntime(399): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtablayout/com.example.androidtablayout.SongsActivity}: java.lang.ClassCastException: android.widget.LinearLayout
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.widget.TabHost.setCurrentTab(TabHost.java:323)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.view.View.performClick(View.java:2408)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.view.View$PerformClick.run(View.java:8816)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.os.Handler.handleCallback(Handler.java:587)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.os.Handler.dispatchMessage(Handler.java:92)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.os.Looper.loop(Looper.java:123)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at java.lang.reflect.Method.invokeNative(Native Method)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at java.lang.reflect.Method.invoke(Method.java:521)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at dalvik.system.NativeStart.main(Native Method)
    03-25 21:01:37.913: E/AndroidRuntime(399): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
    03-25 21:01:37.913: E/AndroidRuntime(399):  at com.example.androidtablayout.SongsActivity.onCreate(SongsActivity.java:36)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    03-25 21:01:37.913: E/AndroidRuntime(399):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    03-25 21:01:37.913: E/AndroidRuntime(399):  ... 18 more
    03-25 21:42:25.942: E/AndroidRuntime(524): FATAL EXCEPTION: main
    03-25 21:42:25.942: E/AndroidRuntime(524): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtablayout/com.example.androidtablayout.SongsActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.widget.TabHost.setCurrentTab(TabHost.java:323)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.view.View.performClick(View.java:2408)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.view.View$PerformClick.run(View.java:8816)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.os.Handler.handleCallback(Handler.java:587)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.os.Handler.dispatchMessage(Handler.java:92)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.os.Looper.loop(Looper.java:123)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at java.lang.reflect.Method.invokeNative(Native Method)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at java.lang.reflect.Method.invoke(Method.java:521)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at dalvik.system.NativeStart.main(Native Method)
    03-25 21:42:25.942: E/AndroidRuntime(524): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.view.ViewGroup.addView(ViewGroup.java:1865)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.view.ViewGroup.addView(ViewGroup.java:1822)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.view.ViewGroup.addView(ViewGroup.java:1802)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at com.example.androidtablayout.SongsActivity.onCreate(SongsActivity.java:51)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    03-25 21:42:25.942: E/AndroidRuntime(524):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    03-25 21:42:25.942: E/AndroidRuntime(524):  ... 18 more

2 个答案:

答案 0 :(得分:0)

您正尝试在上一行之上添加一行,这就是您遇到运行时错误的原因。 首先删除你的第一行,然后添加另一行。堆栈跟踪时的错误很明显。

java.lang.IllegalStateException:指定的子级已有父级。您必须首先在孩子的父母上调用removeView()

答案 1 :(得分:0)

所以,我不知道你想要完成什么,但它正在崩溃,因为你试图将ViewFlipper添加到TableRow,但它已被“添加”到活动布局(在xml中)

这就是这一行:rw1.addView(c);