当我们想要更改活动时,我们可以使用:
Intent i = new Intent(this، ActivityTwo.class);
startActivity(i);
当我在某些Activity中使用它时,它可以正常工作 但是当我在导航抽屉项目中使用它时它不起作用。我认为这不起作用因为测试类扩展了 Fragment 。在 AppCompatActivity 中,它可以正常工作。
public class Test extends Fragment {
public Test() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment_test , container, false);
button= (Button) v.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent((getActivity(), ActivityTwo.class);
startActivity(i);
}
});
return v;
}
}
在我的MainActivity中,我更改了代码:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment fragment=null;
if (id == R.id.nav_camera) {
Test contactUsFragment=new Test();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout,contactUsFragment).commit();
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
ActivityTwoss=new ActivityTwo();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout,ss).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
答案 0 :(得分:1)
您是否创建了一个xml容器?如果是这样,请为FragmentManager使用Fragment而不是Activity。
在我的MainActivity中,我更改了代码:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment fragment=null;
if (id == R.id.nav_camera) {
Test contactUsFragment=new Test();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout,contactUsFragment).commit();
} else if (id == R.id.nav_gallery) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.fragment_conteiner, new Test(), "Test");
transaction.commit();
} else if (id == R.id.nav_slideshow) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
MainActivity xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.onuse.finds.Activity.MainActivity"
tools:showIn="@layout/fragment_conteiner"
android:id="@+id/fragment_conteiner">
</FrameLayout>
答案 1 :(得分:-1)
我想你想用你的片段从你的片段开始一个新的活动:
Intent i = new Intent((getActivity(), ActivityTwo.class);
startActivity(i);
嗯,这不会奏效。您应该从当前活动(而非Activity
)开始Fragment
。你可以使用这样的东西:
Intent i = new Intent((getActivity(), ActivityTwo.class);
getActivity().startActivity(i);
或者您可以在Fragment
:
getActivity().startActivity(new Intent(getActivity(), ActivityTwo.class));
请告诉我这是否适合您!