指定的小孩已经有一个父。您必须在孩子的父母first1调用removeView() [英] The specified child already has a parent. You must call removeView() on the child's parent first1

查看:140
本文介绍了指定的小孩已经有一个父。您必须在孩子的父母first1调用removeView()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code代表的logcat请帮助。当我点击运行这些错误apear即使我搜索一些东西了每次。

This is the code for Logcat please help. When i click run these errors apear everytime even though i searched some stuff up.

        10-03 16:27:07.114: D/AndroidRuntime(7652): Shutting down VM
10-03 16:27:07.114: W/dalvikvm(7652): threadid=1: thread exiting with uncaught exception (group=0x41271930)
10-03 16:27:07.114: E/AndroidRuntime(7652): FATAL EXCEPTION: main
10-03 16:27:07.114: E/AndroidRuntime(7652): java.lang.RuntimeException: Unable to start activity ComponentInfo{wagr.ftc.cascade_app/wagr.ftc.cascade_app.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

这是我的实际code

and this is my actual code

package wagr.ftc.cascade_app;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.NumberPicker;



public class MainActivity extends Activity {

    private AutonomousFragment autoFrag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // set action bar
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);

        autoFrag = new AutonomousFragment();

        FragmentManager fM = getFragmentManager();
        FragmentTransaction fT = fM.beginTransaction();
        fT.add(R.id.container,autoFrag );
        fT.commit();

//        //add tabs
//          actionBar.addTab(actionBar.newTab()
//                  .setText("General")
//                  .setTabListener(new CustomTabListener<AutonomousFragment>(autoFrag,this,AutonomousFragment.class)));
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public static class AutonomousFragment extends Fragment{

        private NumberPicker rampPicker, ballsGoalPicker;

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
        }

        @Override
         public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_main, container);
            rampPicker = (NumberPicker) v.findViewById(R.id.Rolling_Goal_Number_Picker);
            ballsGoalPicker = (NumberPicker)v.findViewById(R.id.Ramp_Rolling_Goals_Number_Picker);

            rampPicker.setMaxValue(3);
            rampPicker.setMinValue(0);
            ballsGoalPicker.setMaxValue(2);
            ballsGoalPicker.setMinValue(0);

            return v;
        }
    }
}

谁能告诉我为什么我的心不是应用程序正常运行?
进出口新的这一点,需要帮助。

Can anyone please tell me why my app isnt running correctly? Im new to this and need help.

推荐答案

问题是你在这条线膨胀您的视图的方式:

The problem is the way you are inflating your view on this line:

View v = inflater.inflate(R.layout.fragment_main, container);

最常见的两种膨胀()方法<一href=\"http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int,%20android.view.ViewGroup)\"相对=nofollow> 膨胀(INT,ViewGroup中) 和<一个href=\"http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int,%20android.view.ViewGroup,%20boolean)\"相对=nofollow> 膨胀(INT,ViewGroup中,布尔) 。这第三个参数是重要的 - 如果你把它设置为true,那么你膨胀的布局将被连接到通过的ViewGroup 作为第二个参数。如果设置为false,则布局吹气将只使用第二个参数给新的布局是一组的LayoutParams

The two most common inflate() methods are inflate(int, ViewGroup) and inflate(int, ViewGroup, boolean). That third parameter is important- if you set it to true, then the layout that you inflate will be attached to the ViewGroup passed as the second parameter. If it is set to false, then the layout inflater will only use the second parameter for giving the new layout a set of LayoutParams.

如果您使用这两个参数膨胀()并传递一个非空的ViewGroup ,然后充气就好像你使用的三个参数充气查看自动附加()并通过真正作为第三个参数。

If you use the two argument inflate() and pass in a non-null ViewGroup, then the inflated View is automatically attached as if you had used the three argument inflate() and passed true as the third argument.

这是很重要的,因为在这里,当你 onCreateView()返回查看,Android将尝试返回查看连接该片段的布局。不过,既然你使用的两个参数的方法,你的虚增布局已经连接到另一个父。

This is important here because when your onCreateView() returns a View, Android will attempt to attach the returned View as the Fragment's layout. However, since you used the two argument method, your inflated layout was already attached to another parent.

切换该行以下内容,你应该罚款。

Switch that line to the following, and you should be fine.

View v = inflater.inflate(R.layout.fragment_main, container, false);

这篇关于指定的小孩已经有一个父。您必须在孩子的父母first1调用removeView()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆