这是Logcat的代码请帮忙。当我点击每次运行这些错误时,即使我搜索了一些东西。
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;
}
}
}
任何人都可以告诉我为什么我的应用程序运行不正常? 我是新手,需要帮助。
答案 0 :(得分:4)
问题是你在这条线上夸大观点的方式:
View v = inflater.inflate(R.layout.fragment_main, container);
两种最常见的inflate()
方法是inflate(int, ViewGroup)
和inflate(int, ViewGroup, boolean)
。第三个参数很重要 - 如果将其设置为true,那么您膨胀的布局将附加到作为第二个参数传递的ViewGroup
。如果设置为false,则布局inflater将仅使用第二个参数为新布局提供一组LayoutParams
。
如果您使用两个参数inflate()
并传入非空ViewGroup
,那么膨胀的视图会自动附加,就像您使用了三个参数inflate()
并传递{ {1}}作为第三个参数。
这一点非常重要,因为当您的true
返回视图时,Android会尝试将返回的视图作为片段的布局附加。但是,由于您使用了两个参数方法,因此您的膨胀布局已经附加到另一个父级。
将该行切换为以下内容,您应该没问题。
onCreateView()
答案 1 :(得分:1)
我认为堆栈跟踪不完整,查看完整堆栈会很有用。
无论如何,虽然我实际上没有尝试过您的代码,但由于以下原因,您可能会遇到此错误:
View v = inflater.inflate(R.layout.fragment_main, container);
如果您导航到inflate实现,它看起来像:
/**
* Inflate a new view hierarchy from the specified xml node. Throws
* {@link InflateException} if there is an error. *
* <p>
* <em><strong>Important</strong></em> 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);