如何在扩展 "Fragment" 的主 class 中扩展 Activity?

How to extend Activity in main class that extends "Fragment"?

我有一个 Android 项目,我在其中使用了 Navigation Drawer,在 MainActivity 中,我有一个 switch-case,有选项和你各自的位置,当用户点击。但是,在名为 TelaQUEMSOMOS 的 class 中,存在的参数:

public class TelaQUEMSOMOS extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.telaquemsomos, container, false);
        return rootView;
    }
}

是从 Fragment 而不是 Activity 延伸的。这很糟糕,因为我需要放一个 Toast.makeText 或 findViewById,但我不能。我该如何处理?

Link 下载我的项目(项目是葡萄牙语):https://goo.gl/pm5dIC

在您的片段中,您可以调用 getActivity() 来获取 activity 的实例。

请检查下面的代码。首先我们会有一个 activity Home.java

final String[] fragments = {
        "com.locate.Establishment", "com.locate.Settings"};
ActionBarDrawerToggle mDrawerToggle;
DrawerLayout drawer;

public class Home extends Home {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
   .
   .//your other code here. 
   .
   // Here I am going to call the Fragment called Establishment
   FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
   tx.replace(R.id.main, Fragment.instantiate(Home.this, fragments[0]));
   tx.commit();
   setToolbar(0);
 }
}

片段的实现。这里我要用户支持Fragment V4.

public class Establishment extends android.support.v4.app.Fragment{
SupportMapFragment mapFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_establishment, null);
    mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
//Now will able to all you need here. By using getActivity().
}
}