2014-10-03 111 views
-2

这是Logcat的代码,请大家帮忙。当我点击每次运行这些错误apear时,即使我搜索了一些东西了。指定的孩子已经有父母。你必须先调用孩子父母的removeView()1

 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. 

,这是我的实际代码

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; 
     } 
    } 
} 

谁能告诉我为什么我的心不是应用程序正常运行? 林新来此,需要帮助。

回答

4

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

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

最常见的两种inflate()方法是inflate(int, ViewGroup)inflate(int, ViewGroup, boolean)。第三个参数很重要 - 如果将其设置为true,则将膨胀的布局附加到作为第二个参数传递的ViewGroup。如果它设置为false,则布局充气器将仅使用第二个参数来为新布局提供一组LayoutParams

如果使用两个参数inflate(),并通过在一个非空ViewGroup,然后膨胀的观点是自动,如果你已经使用的三个参数inflate()并通过true作为第三个参数连接。

这很重要,因为当您的onCreateView()返回一个View时,Android会尝试将返回的View附加为Fragment的布局。但是,由于您使用了两个参数方法,因此您的虚拟布局已经附加到另一个父级。

将该行切换到以下,你应该没问题。

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

在什么情况下子视图有父视图吗? – 2015-07-22 15:41:07

+1

那么,你的观点总是有一个父母。如果您问什么时候想要将充气View附加到家长,那么有几种情况。当ListView或RecyclerView不合适时,如果要将视图充气并将其添加到父级(如LinearLayout),这非常有用。它在创建自定义视图时也很有用,并且您希望为自定义视图提供一个以''标记作为其根的布局。 – 2015-07-22 17:55:50

1

我相信堆栈跟踪是不完整的,看到完整的堆栈将是有用的。

不管怎么说,我并没有真正尝试你的代码,有可能你有这个错误是由于:

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

如果您导航到充气的实现,它看起来是这样的:

/** 
* Inflate a new view hierarchy from the specified xml node. Throws 
* {@link InflateException} if there is an error. * 
* <p> 
* <em><strong>Important</strong></em>&nbsp;&nbsp;&nbsp;For performance 
* reasons, view inflation relies heavily on pre-processing of XML files 
* that is done at build time. Therefore, it is not currently possible to 
* use LayoutInflater with an XmlPullParser over a plain XML file at runtime. 
* 
* @param parser XML dom node containing the description of the view 
*  hierarchy. 
* @param root Optional view to be the parent of the generated hierarchy. 
* @return The root View of the inflated hierarchy. If root was supplied, 
*   this is the root View; otherwise it is the root of the inflated 
*   XML file. 
*/ 
public View inflate(XmlPullParser parser, ViewGroup root) { 
    return inflate(parser, root, root != null); 
} 

所以,如果更换由下面的一个行,它可能工作:

View v = inflater.inflate(R.layout.fragment_main, container, false); 
相关问题