所以我正在研究这个Android应用程序,我把NavigatonDrawer
放进去,这很好用。当选择抽屉中的项目时,MainActivity应该替换FrameLayout
,activity_main.xml
在FrameLayout
中定义。问题是,当我用片段替换 @Override
public void onNavigationDrawerItemSelected(int position) {
FragmentTransaction transaction=getFragmentManager().beginTransaction();
switch (position){
case 0: //cal
calFragment cal=new calFragment().newInstance();
transaction.replace(R.id.container, cal);
break;
case 1: //note
noteFragment note=new noteFragment();
transaction.replace(R.id.container, note);
break;
case 2: //con
conFragment con=new conFragment();
transaction.replace(R.id.container, con);
break;
case 3: //TODO login
break;
case 4: //TODO settings
break;
default:break;
}
transaction.commit();
}
时,app force会关闭。我究竟做错了什么?我该如何解决这个问题?
这里是从抽屉中选择项目时调用的方法:
<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="com.arandum.syncc.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" />
<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.arandum.syncc.NavigationDrawerFragment" />
这是activity_main.xml:
public class calFragment extends Fragment {
private OnFragmentInteractionListener mListener;
public static calFragment newInstance() {
calFragment fragment = new calFragment();
return fragment;
}
public calFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_cal, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(Uri uri);
}
还有calFragment.java和fragment_cal.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="com.arandum.syncc.calFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title_section1"
android:textSize="30sp"
/>
}
04-15 11:23:49.385 22325-22325/com.arandum.syncc D/dalvikvm﹕ Late-enabling CheckJNI
04-15 11:23:49.445 22325-22325/com.arandum.syncc D/dalvikvm﹕ DexOpt: couldn't find field Landroid/app/ActivityThread;.mActiveResources
04-15 11:23:49.445 22325-22325/com.arandum.syncc W/dalvikvm﹕ VFY: unable to resolve instance field 1
04-15 11:23:49.445 22325-22325/com.arandum.syncc D/dalvikvm﹕ VFY: replacing opcode 0x54 at 0x0006
04-15 11:23:49.445 22325-22325/com.arandum.syncc D/dalvikvm﹕ DexOpt: couldn't find field Landroid/app/ActivityThread;.mPackages
04-15 11:23:49.445 22325-22325/com.arandum.syncc W/dalvikvm﹕ VFY: unable to resolve instance field 4
04-15 11:23:49.445 22325-22325/com.arandum.syncc D/dalvikvm﹕ VFY: replacing opcode 0x54 at 0x0000
04-15 11:23:49.560 22325-22325/com.arandum.syncc D/AndroidRuntime﹕ Shutting down VM
04-15 11:23:49.560 22325-22325/com.arandum.syncc W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41fd9ba8)
04-15 11:23:49.565 22325-22325/com.arandum.syncc E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.arandum.syncc, PID: 22325
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arandum.syncc/com.arandum.syncc.MainActivity}: java.lang.ClassCastException: com.arandum.syncc.MainActivity@42315fe8 must implement OnFragmentInteractionListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5029)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: com.arandum.syncc.MainActivity@42315fe8 must implement OnFragmentInteractionListener
at com.arandum.syncc.calFragment.onAttach(calFragment.java:36)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:849)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:6050)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5029)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
04-15 11:23:51.490 22325-22325/com.arandum.syncc I/Process﹕ Sending signal. PID: 22325 SIG: 9
提前致谢!
新
{{1}}
答案 0 :(得分:4)
要拨打FragmentTransaction
并添加/替换/显示/隐藏 Fragment
,您需要按以下方式执行commit
:
fragmentManager.beginTransaction().replace(R.id.container, cal).commit();
然后,您的代码应使用Fragment的常规变量:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransac = fragmentManager.beginTransaction();
// init the fragment (with the a default fragment, not null)
Fragment frag = new calFragment().newInstance();
switch (position){
case 0:
// init with the 1st fragment
frag = new calFragment().newInstance();
break;
case 1:
// init with the 2nd fragment
frag = new noteFragment();
break;
case 2:
// same for others
frag = new conFragment();
break;
case 3: //TODO login
break;
case 4: //TODO settings
break;
default:break;
}
// THEN replace with the fragment variable
fragmentTransac.replace(R.id.container, frag, null);
// commit the transaction
fragmentTransac.commit();
正如您的Logcat所说:com.arandum.syncc.MainActivity@42316ed0 must implement OnFragmentInteractionListener
然后尝试添加此实现:
public class MainActivity extends FragmentActivity (or ActionBarActivity) implements calFragment.OnFragmentInteractionListener {
对于每个Fragment将使用一个,您必须在Interface
内实现自己的MainActivity
,如下所示:
implements calFragment.OnFragmentInteractionListener, noteFragment.PublicInterface, conFragment.AnotherInterface, ...etc
答案 1 :(得分:0)
你需要
FramentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.commit();
要将事务应用于活动,必须调用commit()。
@Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getFragmentManager();
FramentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (position){
case 0: //cal
calFragment cal=new calFragment().newInstance();
fragmentTransaction.replace(R.id.container, cal);
fragmentTransaction.commit();
break;
case 1: //note
noteFragment note=new noteFragment();
fragmentTransaction.replace(R.id.container, note);
fragmentTransaction.commit();
break;
case 2: //con
conFragment con=new conFragment();
fragmentTransaction.replace(R.id.container, con);
fragmentTransaction.commit();
break;
case 3: //TODO login
break;
case 4: //TODO settings
break;
default:break;
}