在我的片段类代码中,我已经实现了一切..但是在从 xml 膨胀它的同时,fiving inflateexceptiom ......
片段类:
package com.nyural.calendar; import java.util.Calendar; import java.util.Currency; import java.util.Locale; import com.nyurals.school.R; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.GestureDetector; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.View.OnTouchListener; import android.widget.ArrayAdapter; import android.widget.GridView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.ViewSwitcher; public class CalendarWeeklyView extends Fragment { private GridView calendarGrid; private ViewSwitcher calendarSwitcher; private Calendar weekCalendar; private Locale locale; private CalendarAdapterWeekly calendarAdapterWeekly; private TextView currentWeek; private GestureDetector swipeDetector; private RelativeLayout calendarLayoutWeekly; public CalendarWeeklyView() { try { weekCalendar = Calendar.getInstance(); locale = Locale.getDefault(); } catch(Exception ex) { } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { try { calendarLayoutWeekly = (RelativeLayout) inflater.inflate(R.layout.calender_weekly,null); GridView calendarDayGridWeekly = (GridView) calendarLayoutWeekly.findViewById(R.id.calendar_days_grid_weekly); swipeDetector = new GestureDetector(getActivity(),new SwipeGesture(getActivity())); calendarGrid = (GridView) calendarLayoutWeekly.findViewById(R.id.calendar_grid_weekly); calendarSwitcher = (ViewSwitcher) calendarLayoutWeekly.findViewById(R.id.calendar_switcher_weekly); currentWeek = (TextView) calendarLayoutWeekly.findViewById(R.id.currentWeekDateWithMonth); calendarAdapterWeekly = new CalendarAdapterWeekly(getActivity()); calendarGrid.setAdapter(calendarAdapterWeekly); updateCurrentWeek(); calendarDayGridWeekly.setAdapter(new ArrayAdapter<String> (getActivity(),R.layout.calendar_item_week,getResources().getStringArray(R.array.weekly_days_array))); calendarGrid.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return swipeDetector.onTouchEvent(event); } }); //return calendarLayoutWeekly; } catch(Exception ex) { } return calendarLayoutWeekly; } private void updateCurrentWeek() { try { //calendarAdapterWeekly.refreshDays(); currentWeek.setText(String.format(locale, "%tm" , weekCalendar) + "/" + weekCalendar.get(Calendar.DATE)); currentWeek.append("-" + String.format(locale, "%tm", weekCalendar) + "/" + weekCalendar.get(Calendar.DATE + 7)); } catch(Exception ex) { } } private void onPreviousWeek() { calendarSwitcher.setInAnimation(getActivity(), R.anim.in_from_left); calendarSwitcher.setOutAnimation(getActivity(), R.anim.out_to_right); calendarSwitcher.showPrevious(); if (weekCalendar.get(Calendar.MONTH) == Calendar.JANUARY) { weekCalendar.set((weekCalendar.get(Calendar.YEAR) - 1), Calendar.DECEMBER, 1); } else { weekCalendar.set(Calendar.MONTH, weekCalendar.get(Calendar.MONTH)-1); //weekCalendar.set(Calendar.WEEK_OF_MONTH, weekCalendar.get(Calendar.WEEK_OF_MONTH) - 7); } updateCurrentWeek(); } private void onNextWeek() { calendarSwitcher.setInAnimation(getActivity(),R.anim.in_from_right); calendarSwitcher.setOutAnimation(getActivity(),R.anim.out_to_left); calendarSwitcher.showNext(); if(weekCalendar.get(Calendar.MONTH) == Calendar.DECEMBER){ weekCalendar.set((weekCalendar.get(Calendar.YEAR) + 1),Calendar.JANUARY,1); } else { weekCalendar.set(Calendar.MONTH , weekCalendar.get(Calendar.MONTH) + 1); //weekCalendar.set(Calendar.WEEK_OF_MONTH, weekCalendar.get(Calendar.WEEK_OF_MONTH) + 7); } updateCurrentWeek(); } private final class SwipeGesture extends SimpleOnGestureListener { private final int swipeMinDistance; private final int swipeThresholdVelocity; public SwipeGesture(Context context) { final ViewConfiguration viewConfig = ViewConfiguration.get(context); swipeMinDistance = viewConfig.getScaledTouchSlop(); swipeThresholdVelocity = viewConfig.getScaledMinimumFlingVelocity(); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if(e1.getX() - e2.getX() > swipeMinDistance && Math.abs(velocityX) > swipeThresholdVelocity) { onNextWeek(); } else if (e2.getX() - e1.getX() > swipeMinDistance && Math.abs(velocityX) > swipeThresholdVelocity) { onPreviousWeek(); } return false; } } }
- 现在我在 actvity_timetable.xml 中使用这个类,
<fragment android:id="@+id/fragmentForTimeTable" android:layout_width="match_parent" android:layout_height="wrap_content" class="com.nyural.calendar.CalendarWeeklyView" />
然后,我在我的主要活动中使用这个 xml,
public class TimeTableActivity extends FragmentActivity { @Override protected void onCreate(Bundle arg0) { setContentView(R.layout.activity_timetable); super.onCreate(arg0); } }
这里是 calendar_weekly
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:id="@+id/calendar_header" android:layout_width="match_parent" android:layout_height="44dp" android:layout_alignParentTop="true" android:background="@android:color/white" > <TextView android:id="@+id/currentWeekDateWithMonth" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="01/01 - 01/08" /> </RelativeLayout> <GridView android:id="@+id/calendar_days_grid_weekly" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/calendar_header" android:background="@android:color/holo_purple" android:horizontalSpacing="1dp" android:numColumns="8" android:stretchMode="columnWidth" /> <ViewSwitcher android:id="@+id/calendar_switcher_weekly" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/calendar_days_grid_weekly" android:background="@android:color/darker_gray" > <GridView android:id="@+id/calendar_grid_weekly" android:layout_width="fill_parent" android:layout_height="fill_parent" android:horizontalSpacing="1dp" android:numColumns="8" android:stretchMode="columnWidth" android:verticalSpacing="1dp" /> </ViewSwitcher> </RelativeLayout>
这是堆栈跟踪...
09-26 08:04:32.820: E/AndroidRuntime(3038): FATAL EXCEPTION: main
09-26 08:04:32.820: E/AndroidRuntime(3038): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nyurals.school/com.nyurals.school.TimeTableActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.os.Looper.loop(Looper.java:137)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-26 08:04:32.820: E/AndroidRuntime(3038): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 08:04:32.820: E/AndroidRuntime(3038): at java.lang.reflect.Method.invoke(Method.java:525)
09-26 08:04:32.820: E/AndroidRuntime(3038): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-26 08:04:32.820: E/AndroidRuntime(3038): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-26 08:04:32.820: E/AndroidRuntime(3038): at dalvik.system.NativeStart.main(Native Method)
09-26 08:04:32.820: E/AndroidRuntime(3038): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-26 08:04:32.820: E/AndroidRuntime(3038): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.app.Activity.setContentView(Activity.java:1895)
09-26 08:04:32.820: E/AndroidRuntime(3038): at com.nyurals.school.TimeTableActivity.onCreate(TimeTableActivity.java:15)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.app.Activity.performCreate(Activity.java:5133)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-26 08:04:32.820: E/AndroidRuntime(3038): ... 11 more
09-26 08:04:32.820: E/AndroidRuntime(3038): Caused by: java.lang.ClassCastException: com.nyural.calendar.CalendarWeeklyView cannot be cast to android.app.Fragment
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.app.Fragment.instantiate(Fragment.java:585)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.app.Fragment.instantiate(Fragment.java:560)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.app.Activity.onCreateView(Activity.java:4738)
09-26 08:04:32.820: E/AndroidRuntime(3038): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
09-26 08:04:32.820: E/AndroidRuntime(3038): ... 21 more
为什么它给出那个例外????请尽快回复,因为我无法解决这个问题......