我想从导航抽屉里面的HomeF.java创建一个mapaQRF.java的startActivity。 我的意思是,我希望onclick(按钮)和onNavigationDrawerItemSelected一样(在MapaQRF pocision中)
fragment_home.xml
<FrameLayout 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" tools:context="cl.bikepurranque.www.bikeqrf.HomeF">
<Button
android:layout_width="190dp"
android:layout_height="wrap_content"
android:text="Mapa Paraderos QR"
android:id="@+id/btnMap"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:onClick="onClickMap"/>
</FrameLayout>
HomeF.java(这是问题所在)
public void onClickMap(View view){
Intent intent = new Intent(this.getActivity(), MapaQRF.class);
startActivity(intent);
}
NavDrawer.java
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = null;
switch (position){
case 0:
fragment = new MapaQRF();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
break;
case 1:
fragment = new DetectarQRF();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
break;
case 2:
Intent intent = new Intent(this, Login.class);
startActivity(intent);
break;
}
}
问题是当我需要调用片段时调用一个活动。
删除:按钮上的“android:onClick =”onClickMap“”(fragment_home.xml)
在HomeF.java中添加
(onCreateView)方法
btnMap =(Button)rootView.findViewById(R.id.btnMap);
btnMap.setOnClickListener(本);
@Override
public void onClick(View v) {
if (v == btnMap){
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
android.support.v4.app.Fragment fragment = null;
fragment = new MapaQRF();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
}
答案 0 :(得分:0)
尝试使用getActivity()
public void onClickMap(View view){
Intent intent = new Intent(getActivity(), MapaQRF.class);
getActivity().startActivity(intent);
}