Java 有没有办法在片段中显示活动

Java 有没有办法在片段中显示活动,java,android,xml,android-fragments,Java,Android,Xml,Android Fragments,我在地图应用程序中使用导航抽屉,我设法在主页上显示地图。我遇到的问题是,我无法将布局xml中的映射片段连接到MapsActivity java文件 我尝试使用MapsActivity extends Fragment代替MapsActivity extends FragmentActivity,但这当然不起作用 主要的xml布局: <!-- A DrawerLayout is intended to be used as the top-level content view using m

我在地图应用程序中使用导航抽屉,我设法在主页上显示地图。我遇到的问题是,我无法将布局xml中的映射片段连接到MapsActivity java文件

我尝试使用MapsActivity extends Fragment代替MapsActivity extends FragmentActivity,但这当然不起作用

主要的xml布局:

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


 <!-- *****The problem I am having is in the following fragment could not link it to the MapsActivity-->
 <fragment
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment"/>


</FrameLayout>


<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.example.famfelimban.mydiet.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />
不,Activity是片段的持有者,而片段不能持有Activity


这个问题可能很有意思:

这里要做的是将活动代码移植到一个片段中。这并不难,一个片段有一个几乎相同的生命周期。唯一的区别是,您不需要在onCreate中获取和修改视图,而需要在onCreateView中执行此操作,并且需要实现一个静态newInstance方法

将xml中的FrameLayout保留为空。只有碎片管理器才能将碎片膨胀成碎片。代码的两个更改

在活动中

并制作一个功能类似于活动版本的片段


祝你好运。

是的,我知道,但是还有别的办法吗?浏览了2天,没有发现任何需要回复的内容,那么你的意思是我的活动中只有onStart,然后将活动中的所有方法实现到片段中?onCreate仍然存在。setContentView仍然需要调用,您可以在onCreate中执行片段事务,但活动的主要任务是启动执行所有繁重任务的片段。
<fragment
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:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
    public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {

    }

    private void setUpMapIfNeeded() {

    }

    private void setUpMap() {

    }
}
@Override
protected void onStart() {
    super.onStart();

    Fragment mapFragment = MapFragment.newInstance(/*args*/);
    getFragmentManager()  //you may need getSupportFragmentMananger()
            .beginTransaction()
            .replace(R.id.container, mapFragment)
            .commit()
    //this inflates the Fragment into the FrameLayout
}
public class MapFragment extends Fragment {
    ...
    public MapFragment() {/*should be empty*/}
    ...
    public static MapFragment newInstance(/*args*/) {
        Fragment fragment = new MapFragment();

        Bundle args = new Bundle();
        //put whatever information you need for your activity to run here
        fragment.setArguments(args);
        return fragment;
    }
    ...
    @Overide
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        //this basically takes the place of onCreate in the Activity version
        View root = inflater.inflate(R.layout.fragment_map, parent);

        View yourView = root.findViewById(R.id.your_view);
        ...
        return root;
    }
    ...
}