Java Can';t从活动开始片段

Java Can';t从活动开始片段,java,android,overlapping,Java,Android,Overlapping,我试图从活动中开始一个片段,但它不起作用,有人能帮忙吗 这是main.java public class MainActivity extends FragmentActivity { Button clickButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCont

我试图从活动中开始一个片段,但它不起作用,有人能帮忙吗

这是main.java

public class MainActivity extends FragmentActivity {

    Button clickButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        clickButton = (Button)findViewById(R.id.button1);
    click();
    }

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

    private void click()
    {
        clickButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.i("click","clickt");
             FragmentOne fragmentOne = new FragmentOne();
                 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                 transaction.replace(R.id.fragmentcontainer, fragmentOne);
                 transaction.addToBackStack(null);
                 transaction.commit();
            }

        });
    }
这是主布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testfragment.MainActivity" >



     <FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" 
    android:id="@+id/fragmentcontainer">
</FrameLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="57dp"
        android:text="Button" />

</RelativeLayout>
public class FragmentOne extends Fragment  {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.signup_, container,false);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="TextView" />

</LinearLayout>
这是片段布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testfragment.MainActivity" >



     <FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" 
    android:id="@+id/fragmentcontainer">
</FrameLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="57dp"
        android:text="Button" />

</RelativeLayout>
public class FragmentOne extends Fragment  {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.signup_, container,false);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="TextView" />

</LinearLayout>

暂时尝试main.xml的这种布局,并告诉我结果

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testfragment.MainActivity" >

    <FrameLayout
        android:id="@+id/fragmentcontainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5" >
    </FrameLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1" 
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
       android:layout_height="0dp"
        android:layout_weight="1" 
        android:text="Button" />

</LinearLayout>

  • 问题是FrameLayout的宽度为0
更改此项:

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" 
    android:id="@+id/fragmentcontainer">
</FrameLayout>

为此:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragmentcontainer">
</FrameLayout>


解释:权重属性是
线性布局
的布局参数,而不是
相对布局
。布局参数是指
视图
/
视图组
的容器(在这种情况下,
视图
/
视图组
是您的
框架布局
)。因此,这里基本上完全忽略了权重,将宽度设置为0,使得
FrameLayout
片段
视图实际上不可见

请粘贴logcathere@ConnectinglifewithAndroidlogcat中没有错误:(只需粘贴您的日志猫…即使您没有看到任何错误…单击按钮,然后粘贴日志猫09-05 13:48:10.689:W/IIInputConnectionWrapper(29080):获取非活动InputConnection上的SelectedText 09-05 13:48:10.689:W/IIInputConnectionWrapper(29080):设置非活动InputConnection上的合成文本09-05 13:48:20.049:E/SpanBalstringBuilder)(29080):SPAN_EXCLUSIVE_EXCLUSIVE SPAN不能为零长度09-05 13:48:20.049:E/SpannableStringBuilder(29080):SPAN_EXCLUSIVE_EXCLUSIVE SPAN不能为零长度09-05 13:48:33.769:I/单击(29080):单击t确保在FragmentOne中导入android.support.v4.app.Fragment;而不是导入aswer Gil的android.app.Fragment;thx,但现在to布局重叠。如何仅显示注册?取决于您希望如何放置注册。您可以使用TextView上下面的布局?将其放置在FrameLayout下。高度为tFrameLayout可以设置为根据内容的大小对内容进行换行以调整大小。如果希望尽可能多地填充空间,可以在FrameLayout上使用顶级LinearLayout,并将layout\u weight属性设置为1(但不要像此处那样使用顶级RelativeLayout)。或者,您可以将RelativeLayout与下面/上面/等布局参数很好地结合使用。不过,您可以将其作为另一个问题来提问,这与上面的注释中的问题不同。我的意思是,问题不在于注册.xml,而在于您没有对齐主布局中的视图。请使用RelativeLayout.LayoutParams为此(如下面的/alignParentRight/等)请Gil我不知道怎么做,你能给我一个例子,说明我如何只能显示注册内容吗?你说“仅”注册内容是什么意思?这个布局是否应该被其他视图覆盖,按钮和下面的文本视图?在这种情况下,在下面添加
android:layou=“@+id/碎片容器"
到FrameLayout下面的TextView。但是最好你在这里发布另一个问题,而不是在这里发表评论,这样社区就可以帮助THX找到答案,但是现在这两个xml文件重叠了。我如何才能只显示注册?这个问题主要与你的xml设计有关。虽然调用了片段,但它不是您可以选择ans并为xml设计打开一个新问题