我正在尝试在片段中创建一个caldroid日历。
但是我得到一个错误我做错了什么?我有一个滑动菜单(不是绝对),它似乎导致了我认为的错误。
我已经包含了从下面的主片段中调用的xml和类以及log cat。有谁知道我做错了什么?
public class PM_Fragment extends Fragment{
private FragmentActivity myContext;
@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.pm_fragment, container, false);
CaldroidFragment caldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
Calendar cal = Calendar.getInstance();
args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
caldroidFragment.setArguments(args);
FragmentManager fragManager = myContext.getSupportFragmentManager();
android.support.v4.app.FragmentTransaction t = fragManager.beginTransaction();
t.replace(R.id.pm_fragment, caldroidFragment);
t.commit();
return rootView;
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin" >
<fragment
android:name="com.projectcaruso.naturalfamilyplaning.PM_Fragment"
android:id="@+id/pm_fragment"
android:layout_width="0dp"
android:layout_height="match_parent" />
</RelativeLayout>
错误:
05-29 00:50:00.382: E/AndroidRuntime(11536): FATAL EXCEPTION: main
05-29 00:50:00.382: E/AndroidRuntime(11536): Process: com.projectcaruso.naturalfamilyplaning, PID: 11536
05-29 00:50:00.382: E/AndroidRuntime(11536): android.view.InflateException: Binary XML file line #55: Error inflating class fragment
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
05-29 00:50:00.382: E/AndroidRuntime(11536): at com.projectcaruso.naturalfamilyplaning.CalendarFragment.onCreateView(CalendarFragment.java:29)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.app.Fragment.performCreateView(Fragment.java:1700)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.app.BackStackRecord.run(BackStackRecord.java:684)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.os.Handler.handleCallback(Handler.java:733)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.os.Handler.dispatchMessage(Handler.java:95)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.os.Looper.loop(Looper.java:157)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.app.ActivityThread.main(ActivityThread.java:5356)
05-29 00:50:00.382: E/AndroidRuntime(11536): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 00:50:00.382: E/AndroidRuntime(11536): at java.lang.reflect.Method.invoke(Method.java:515)
05-29 00:50:00.382: E/AndroidRuntime(11536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-29 00:50:00.382: E/AndroidRuntime(11536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-29 00:50:00.382: E/AndroidRuntime(11536): at dalvik.system.NativeStart.main(Native Method)
05-29 00:50:00.382: E/AndroidRuntime(11536): Caused by: java.lang.ClassCastException: com.projectcaruso.naturalfamilyplaning.BaseActivity cannot be cast to android.support.v4.app.FragmentActivity
05-29 00:50:00.382: E/AndroidRuntime(11536): at com.projectcaruso.naturalfamilyplaning.PM_Fragment.onAttach(PM_Fragment.java:22)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:849)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.app.Activity.onCreateView(Activity.java:4972)
05-29 00:50:00.382: E/AndroidRuntime(11536): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
05-29 00:50:00.382: E/AndroidRuntime(11536): ... 19 more
答案 0 :(得分:2)
我正在检查git上的示例,他们为该示例使用了linearLayout而不是
您的xml布局:
<fragment
android:name="com.projectcaruso.naturalfamilyplaning.PM_Fragment"
android:id="@+id/pm_fragment"
android:layout_width="0dp"
android:layout_height="match_parent" />
Caldroid样本布局:
<LinearLayout
android:id="@+id/calendar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
我在哪里获得信息: https://github.com/roomorama/Caldroid/blob/master/demo/res/layout/activity_main.xml
答案 1 :(得分:0)
尝试使用FrameLayout而不是片段