0

I'm calling on this acitvity as my main laucher this activity extends the SlidingActivity from this library https://github.com/jfeinstein10/SlidingMenu. When it creates a behindcontentview for the slidingmenu I use with a fragment. In this main activity I creating a custom view for a calendar there are icons that are drawed in this custom view. I want to toggle these icons so I'm using switches in the sliding menu fragment view but I'm getting a null pointer exception which log cat saids its happens in the listner when i toggle in the fragment where the switch is. So my code to clears the icons on a calendar that is made using the custom view in the main activty and does work with I use the code in the main activity but not within the toggle listner OnCheckedChangeListener.

Heres Mainactivity that calls on the slide menu and fragment to fill the slide menu

    setBehindContentView(R.layout.menu_frame);
    FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
    mFrag = new ColorFragment();
    t.replace(R.id.menu_frame, mFrag);
    t.commit();

    // customize the SlidingMenu
    SlidingMenu sm = getSlidingMenu();
    sm.setShadowWidthRes(R.dimen.shadow_width);
    sm.setShadowDrawable(R.drawable.shadow);
    sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    sm.setFadeDegree(0.35f);
    sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    //sm.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);



    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    //redrawtest();
   // setListener();

Heres my xml for this main activity.

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.projects.shiftcalendar.CalendarView
    android:id="@+id/view_month_calendar"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</com.projects.shiftcalendar.CalendarView>

</LinearLayout>

This is my fragment

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
    return inflater.inflate(R.layout.slide_menu_toggle, null );

}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Switch d = (Switch) getView().findViewById(R.id.monitored_switch1);
    d.setOnCheckedChangeListener(this);


}

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {



    if(isChecked){

        cv.redrawCalendarClearSymbol();



    }
    else {

        cv.redrawCalendarClearSymbol();


      }



  }


}
Ahmad
  • 69,608
  • 17
  • 111
  • 137
madmax_5
  • 1
  • 3

1 Answers1

0

You wrote:

I'm getting a null pointer exception which log cat saids its happens in the listner when i toggle in the fragment where the switch is (...) [doesn't work within the toggle listner OnCheckedChangeListener.

Based on your description, the NullPointerException happens when you call:

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
     //...        
    cv.redrawCalendarClearSymbol();

It seems like cv is null. One possible reason for the NPE is that cv hasn't been instantiated when the Fragment was created. Assuming cv is part of your fragment's layout, you can assign a reference to 'cv' in onCreateView():

YourCustomType cv;//field

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.slide_menu_toggle, null);
    cv = (YourCustomType)vg.findViewById(R.id.yourIdForCVInTheFragmentLayout);
    return vg;
}
Gunnar Karlsson
  • 28,350
  • 10
  • 68
  • 71