2016-07-28 78 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(); 
      } 
     }); 

    } 
} 

有我的片段的任何問題?還是主要活動?請儘快幫助。該應用程序打開,但我試圖點擊日曆按鈕,nothins顯示。

+1

'a = new Activity();'...永遠不要做一個'new Activity()' –

+0

@Mohendra是否有效? – Heisenberg

+0

不,http://stackoverflow.com/questions/38654653/why-is-calendar-in-navigation-drawer-not-working/38654768?noredirect=1#comment64692360_38654768繼承人我已經給了整個代碼的鏈接,請看到它 – 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是日曆。