這是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;
}
}
}
誰能告訴我爲什麼我的心不是應用程序正常運行? 林新來此,需要幫助。
在什麼情況下子視圖有父視圖嗎? – 2015-07-22 15:41:07
那麼,你的觀點總是有一個父母。如果您問什麼時候想要將充氣View附加到家長,那麼有幾種情況。當ListView或RecyclerView不合適時,如果要將視圖充氣並將其添加到父級(如LinearLayout),這非常有用。它在創建自定義視圖時也很有用,並且您希望爲自定義視圖提供一個以''標記作爲其根的佈局。 –
2015-07-22 17:55:50