2016-07-28 3 views
-1

내 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; 
    } 
}); 

를 포함하고 내 Calender.java는

public class Calender extends android.support.v4.app.Fragment{ 
    Activity a; 
    CalendarView calendar; 
    public Calender() { 
     // Required empty public constructor 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     a = new Activity(); 
     a.setContentView(R.layout.fragment_calender); 

     initializeCalendar(); 
     return inflater.inflate(R.layout.fragment_calender, container, false); 

    } 

    public void initializeCalendar() { 

     calendar = (CalendarView) a.findViewById(R.id.calendar); 
     // sets whether to show the week number. 
     calendar.setShowWeekNumber(false); 
     // sets the first day of week according to Calendar. 
     // here we set Monday as the first day of the Calendar 
     calendar.setFirstDayOfWeek(2); 
     //The background color for the selected week. 
     calendar.setSelectedWeekBackgroundColor(getResources().getColor(R.color.green)); 
     //sets the color for the dates of an unfocused month. 
     calendar.setUnfocusedMonthDateColor(getResources().getColor(R.color.transparent)); 
     //sets the color for the separator line between weeks. 
     calendar.setWeekSeparatorLineColor(getResources().getColor(R.color.transparent)); 
     //sets the color for the vertical bar shown at the beginning and at the end of the selected date. 
     calendar.setSelectedDateVerticalBar(R.color.darkgreen); 
     //sets the listener to be notified upon selected date change. 
     calendar.setOnDateChangeListener(new OnDateChangeListener() { 
      @Override 
      public void onSelectedDayChange(CalendarView view, int year, int month, int day) { 
       Toast.makeText(a.getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show(); 
      } 
     }); 

    } 
} 

내 조각에 문제가 포함되어 있습니다? 또는 주요 활동에 있습니까? asap.The 응용 프로그램이 열립니다하지만 캘린더 버튼을 클릭하려고 아무데도 나타납니다.

+1

'a = 새로운 활동();'... 결코 새로운 활동()을 만들지 마십시오. –

+0

@Mohendra가 작동 했습니까? – Heisenberg

+0

아니요, http://stackoverflow.com/questions/38654653/why-is-calendar-in-navigation-drawer-not-working/38654768?noredirect=1#comment64692360_38654768 전체 코드를 제공 한 링크를 heres하십시오. 그것을 보아라. – Mohendra

답변

0

왜 조각에 활동 인스턴스가 있습니까? 에 onCreateView 변경 :

public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    View view = inflater.inflate(R.layout.content_main, parent, false); 
    calendar = (CalendarView) view.findViewById(R.id.calendar); 
    return view; 
} 

다음 onViewCreated 방법에서이 작업을 수행 :

initializeCalendar(); 

확실 content_main에서 캘린더보기의 ID가 달력되어 있는지 확인합니다.

관련 문제