2016-07-29 12 views
0

次は私のカレンダーのための私のコードナビゲーション・ドロワーのカレンダーが機能しないのはなぜですか?

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上のコードで、イムが開くためにフラグメントを作成しようとしていますカレンダーのナビゲーションドロワーから

私はカレンダー用の新しい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> 

答えて

1

断片には方法getApplicationContext()はありません。ローカルコンテキストにアクセスする場合は、getApplicationContext()の代わりにgetActivity()と書いてください。

+0

何それは働いていないされていない、私はあなたがエラーのどのような種類をreveiveんコード – Mohendra

+0

を見てください可能性があり、それが原因の容器、またはフレームレイアウトになるかもしれないと思いますか? –

+0

正確にはエラーではありません。ナビゲーション・ドロワーのカレンダーをクリックすると、何も表示されません。 – Mohendra

0

getActivity().getApplicationContext()

代わりにのみgetApplicationContext()をお試しください。

が、それは変更が必要とされている理由を理解するために

Toast.makeText(getActivity().getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show(); 

に:)

+0

私はそれを試しましたが、何も開きません。アプリケーションは実行されますが、ボタンをクリックしても何も表示されません。 – Mohendra

+0

Try- Toast.makeText getActivity()、day + "/" + month + "/" + year、Toast.LENGTH_LONG).show(); – Neo

0
Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show(); 

変更それを助けることを願って、ここでanswerです。

0

フラグメントメソッドをonAttachでオーバーライドし、フラグメントクラス全体で使用するアクティビティ参照を格納します。

public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    mActivity = activity; 
} 
関連する問題