2014-10-03 61 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); 
相關問題