以下是我的日历代码
public class Calender extends android.support.v4.app.Fragment {
CalendarView calendar;
public Calender() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View fragmentRootView = inflater.inflate(R.layout.fragment_calender, container, false);
calendar = (CalendarView) fragmentRootView.findViewById(R.id.calendar);
calendar.setShowWeekNumber(false);
calendar.setFirstDayOfWeek(2);
calendar.setSelectedWeekBackgroundColor(getResources().getColor(R.color.green));
calendar.setUnfocusedMonthDateColor(getResources().getColor(R.color.transparent));
calendar.setWeekSeparatorLineColor(getResources().getColor(R.color.transparent));
calendar.setSelectedDateVerticalBar(R.color.darkgreen);
calendar.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
}
});
return fragmentRootView;
}
}
getApplicationContext()中存在错误, 以下是MainActivity.java上的代码,我试图创建一个片段,以便从导航抽屉打开日历
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.calendar:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new Calender());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Calender");
item.setChecked(true);
break;
// drawer.closeDrawers();
}
return true;
}
});
我也为日历创建了一个新的xml文件,并在主xml中使用了框架布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorAccent"
app:tabSelectedTextColor="@color/tabTextColor"
app:tabTextColor="#FFF">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="@+id/tab_layout">
</android.support.v4.view.ViewPager>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lvPump"
android:layout_centerHorizontal="true"
android:layout_below="@+id/tab_layout" />
</RelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_container"></FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/navigation_item">
</android.support.design.widget.NavigationView>
答案 0 :(得分:1)
Fragment中没有方法getApplicationContext()
。如果您想要访问本地上下文,请写getActivity()
而不是getApplicationContext()
。
答案 1 :(得分:0)
尝试
getActivity().getApplicationContext()
而不仅仅是getApplicationContext()
。
希望它会有所帮助:)
答案 2 :(得分:0)
Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
将其更改为
Toast.makeText(getActivity().getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
了解为何需要进行更改,此处为answer。
答案 3 :(得分:0)
覆盖片段方法onAttach并存储活动引用以在整个片段类中使用。
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = activity;
}