0

我呼吁这个活动作为我的主要任务,这个活动从这个库https://github.com/jfeinstein10/SlidingMenu扩展了 SlidingActivity 。当它为我与片段一起使用的滑动菜单创建背后内容视图时。在这个主要活动中,我为日历创建了一个自定义视图,在这个自定义视图中绘制了一些图标。我想切换这些图标,所以我在滑动菜单片段视图中使用开关,但我得到一个空指针异常,当我在开关所在的片段中切换时,日志 cat 说它在侦听器中发生。因此,我的代码清除了使用主活动中的自定义视图制作的日历上的图标,并且确实可以与我在主活动中使用代码,但不在切换列表器 OnCheckedChangeListener 中使用。

这是调用幻灯片菜单和片段以填充幻灯片菜单的 Mainactivity

    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();

这是我用于此主要活动的 xml。

 <?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>

这是我的片段

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();


      }



  }


}
4

1 回答 1

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;
}
于 2012-12-17T00:26:29.427 回答