这是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