用户界面-运行后未创建的Android片段



在设置了活动中的内容视图后,我正试图向该活动添加一个片段。我没有使用兼容包,我的目标API设置为16。我希望活动在主活动中设置为setContentView方法之后的片段布局,但在运行时,片段从未添加,屏幕为空白。片段中生命周期事件的回调永远不会被调用,因此System.outs不会输出。Logcat也不会输出任何与片段相关的错误。如果在activity_main.xml中创建了一个fragment元素,则该fragment可以正常工作,但将其添加到类中的RelativeLayout中是不起作用的。有人能帮帮我吗?谢谢编辑:尝试使用FrameLayout作为fragment_container,但没有区别。

这是我的主要活动:

package com.example.derp;  
import android.os.Bundle;  
import android.app.Activity;  
import android.app.Fragment;  
import android.app.FragmentManager;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Fragment frag = new Fragment();
    FragmentManager fManager = getFragmentManager();
    fManager.beginTransaction().add(R.id.fragment_container, frag).commit();
}
}

这是我的片段:

 package com.example.derp;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Frag extends Fragment{
public Frag(){
}
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    System.out.println("onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("onCreate");
}
@Override
public void onStart() {
    super.onStart();
    System.out.println("onStart");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    System.out.println("onCreateView");
    return inflater.inflate(R.layout.fragment_frag, container, false);//
}
}

这是fragment_frag.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />
<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="64dp"
    android:ems="10"
    android:inputType="textPersonName" >
    <requestFocus />
</EditText>
</RelativeLayout>

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >    
<RelativeLayout 
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</RelativeLayout>
</RelativeLayout>

您没有创建片段类的实例。相反,您正在创建基片段类的对象。

更改。。。

Fragment frag = new Fragment();

收件人。。。

Fragment frag = new Frag();

最新更新