1

我正在使用导航抽屉,我想调用testActivity.java类,当有人单击滑动菜单中的项目时。目前我的MainActivity.java calss正在调用FragmentOne, FragmentTwo,FragmentThree滑动时的菜单项selected..I想testActivity.java如何从片段调用活动类

替换此,这里是我MainActivity.java

public class MainActivity extends FragmentActivity { 
final String[] data ={"one","two","three"}; 
final String[] fragments ={ 
     "core.layout.FragmentOne", 
     "core.layout.FragmentTwo", 
     "core.layout.FragmentThree"}; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data); 

    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout); 
    final ListView navList = (ListView) findViewById(R.id.drawer); 
    navList.setAdapter(adapter); 
    navList.setOnItemClickListener(new OnItemClickListener(){ 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){ 
        drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener(){ 
          @Override 
          public void onDrawerClosed(View drawerView){ 
            super.onDrawerClosed(drawerView); 
            FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); 
            tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos])); 
            tx.commit(); 
          } 
        }); 
        drawer.closeDrawer(navList); 
      } 
    }); 
    FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); 
    tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0])); 
    tx.commit(); 
} 

}

这里是我的FragmentOne.java CALSS ..所有其他FragmentTwo和FragmentThr ee.java类具有相同的代码。

 public class FragmentOne extends Fragment { 


public static Fragment newInstance(Context context) { 
    FragmentOne f = new FragmentOne(); 

    return f; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null); 

    View view = inflater.inflate(R.layout.fragment_one,container, false); 

    return root; 


} 

}

这是我testActivity.java类

public class testActivity extends Activity { 

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


} 

}

回答

3

在片段可以使用getActivity访问其父FragmentActivity实例()。而且由于这个实例是一个活动,所以使用它你可以简单地调用另一个活动。

Intent myIntent = new Intent((ParentActivity)getActivity(), YourOtherActivity.class); 
(ParentActivity)getActivity().startActivity(myIntent); 
+0

你的意思是像这样在FragmentOne类?..但它不工作? Intent intent = new Intent(getActivity(),testActivity.class); getActivity()。startActivity(intent); – san88

+0

我已经把它们放在了这个样子上。没有错误或运行时错误。但是根本不显示布局文件。 public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)ViewGroup root =(ViewGroup)inflater.inflate(R.layout.fragment_one,null); 查看视图= inflater.inflate(R.layout.fragment_one,container,false);意图intent = new Intent(getActivity(),testActivity.class); getActivity()。startActivity(intent); return root; } – san88

+0

尝试将您的getActivity转换为ParentActivity。我编辑了我的答案以添加示例代码。 – Sushil