0

i am creating a new android application.i am using the table layout. I have coded as follow but when i run the program it is shows force to close the program. can any one help on this code .

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(c);
        d.addView(rw2);
        d.addView(rw1);
        setContentView(d);
    } 

my layout defination is as follow

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">

<TableRow
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
    android:id="@+id/tableRow1">

    <!-- Screen Design for the SONGS -->
<ViewFlipper android:id="@+id/viewFlipper1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
  </ViewFlipper>

</TableRow>

<TableRow

  android:orientation="vertical"
    android:id="@+id/tableRow2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

</TableRow>
</TableLayout>

my logcat is showing following error

03-24 14:33:50.722: E/AndroidRuntime(322): FATAL EXCEPTION: main
03-24 14:33:50.722: E/AndroidRuntime(322): 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-24 14:33:50.722: E/AndroidRuntime(322):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.widget.TabHost.setCurrentTab(TabHost.java:323)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.view.View.performClick(View.java:2408)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.view.View$PerformClick.run(View.java:8816)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.os.Handler.handleCallback(Handler.java:587)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.os.Looper.loop(Looper.java:123)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-24 14:33:50.722: E/AndroidRuntime(322):  at java.lang.reflect.Method.invokeNative(Native Method)
03-24 14:33:50.722: E/AndroidRuntime(322):  at java.lang.reflect.Method.invoke(Method.java:521)
03-24 14:33:50.722: E/AndroidRuntime(322):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-24 14:33:50.722: E/AndroidRuntime(322):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-24 14:33:50.722: E/AndroidRuntime(322):  at dalvik.system.NativeStart.main(Native Method)
03-24 14:33:50.722: E/AndroidRuntime(322): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.view.ViewGroup.addView(ViewGroup.java:1865)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.view.ViewGroup.addView(ViewGroup.java:1822)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.view.ViewGroup.addView(ViewGroup.java:1802)
03-24 14:33:50.722: E/AndroidRuntime(322):  at com.example.androidtablayout.SongsActivity.onCreate(SongsActivity.java:51)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-24 14:33:50.722: E/AndroidRuntime(322):  ... 18 more
Ashishsingh
  • 742
  • 2
  • 12
  • 31
  • Please provide the stacktrace. – Boris Strandjev Mar 24 '12 at 09:11
  • Also add your layout definition. – Boris Strandjev Mar 24 '12 at 09:12
  • @BorisStrandjev what is stacktrace? – Ashishsingh Mar 24 '12 at 09:16
  • Are you sure you gave us the layout R.layout.songs_layout ? Everything seems alrgiht. Please provide adb logcat output – Snicolas Mar 24 '12 at 09:18
  • @samir how to add logcat file here ? – Ashishsingh Mar 24 '12 at 09:19
  • @snicolas What is adb logcat o/p? – Ashishsingh Mar 24 '12 at 09:20
  • 1
    @Ashishsingh This is the description of the error you get. I am assuming you are using Eclipse: Window -> show view -> Other -> Android -> LogCat(deprecated). There you should see the error you get printed out (a lot of red lines one after the other). A typical stacktrace can be seen here: http://stackoverflow.com/questions/3353023/android-illegalstateexception-when-is-it-thrown – Boris Strandjev Mar 24 '12 at 09:20
  • We want your stacktrace to help you. You must learn to read a proper stack trace in android to program. You can either see it in eclipse or you a command line tool (adb) to get your stack trace. The stack trace will give you the kind of error and which line causes it, there is no alternative to solve a problem : http://developer.android.com/guide/developing/tools/adb.html#logcat – Snicolas Mar 24 '12 at 09:22
  • Cut and past the result of your logcat – Snicolas Mar 24 '12 at 09:22
  • A logcat is typcally read bottom up. Find the topmost line that is part of your code (not android framework) and this will be the place where you do something wrong. Sometimes it's not that easy, in your case it might more complicated as there seem to be something wrong in your layout, but still, have a look at the stack trace and copy it here – Snicolas Mar 24 '12 at 09:24

3 Answers3

1

The view flipper was added to first table row (rw1) and you are trying to add it again to the second table row (rw2). That's why you get an error. This view already has a parent.

Radu Dan
  • 453
  • 4
  • 22
  • when i am changing this with other view then also it is showing an error Warning: Activity not started, its current task has been brought to the front it is getting force to close – Ashishsingh Mar 24 '12 at 09:30
  • Try comment setContentView(d); Do you still get an error? And please change it to another view, don't assign a view child to multiple parents. It's not correct. – Radu Dan Mar 24 '12 at 09:32
  • Put your table layout (from XML file), in a LinearLayout. – Radu Dan Mar 24 '12 at 09:52
  • i want to create two row in my page in which one row will show some pic and other will show 3d image both thing will be done through custom view.that's why i am using the two row. if i ill use linear layout then how can i show the two view – Ashishsingh Mar 24 '12 at 09:59
  • I ran your code and no error what so ever. I replaced your custom views with some normal View Objects. I think, you have a custom view that causes this crash. – Radu Dan Mar 24 '12 at 10:13
  • You can narrow the search by adding a view to your table layout. If it's ok, than add another one, and so on. You can do this until your application crashed and point out the problematical custom view. Also, you may want to post here your custom view class(or on another thread). – Radu Dan Mar 24 '12 at 10:57
0

Ok, rw1 and rw2 already have a parent in your xml file, you can't add them to another container. Don't mix widget positionning by code and by xml. By xml is really the standard and only a few dynamic positionning need a programmatic layout construction.

Start with a simpler example, and only use the xml to build your UI.

@Radu and I have found the error because of the line (find the HERE word)

Caused by: java.lang.IllegalStateException: The specified child already has a parent.     You must call removeView() on the child's parent first.
03-24 14:33:50.722: E/AndroidRuntime(322):  at     android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
03-24 14:33:50.722: E/AndroidRuntime(322):  at     android.view.ViewGroup.addView(ViewGroup.java:1865)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.view.ViewGroup.addView(ViewGroup.java:1822)
03-24 14:33:50.722: E/AndroidRuntime(322):  at android.view.ViewGroup.addView(ViewGroup.java:1802)
 ----> HERE  03-24 14:33:50.722: E/AndroidRuntime(322):  at     com.example.androidtablayout.SongsActivity.onCreate(SongsActivity.java:51)

This line is the topmost line that is yours. That where the error lies. The nature of the error you got is contained in the first line I copied. That's the way a stacktrace works.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • i want to create two row in my page in which one row will show some pic and other will show 3d image both thing will be done through custom view.that's why i am using the two row but it isnot working ? – Ashishsingh Mar 24 '12 at 09:44
0

You are trying to add a view which already has a parent view first you need to remove the parent via using removeView() on parent view.

The stack trace is pretty clear.

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Serdar Dogruyol
  • 5,147
  • 3
  • 24
  • 32