如何从另一个类调用方法滑动菜单?

时间:2015-04-14 07:03:59

标签: android class methods menu sliding

我使用jeremyfeinstein创建菜单,如下所示:     import android.app.Activity;

import com.e_campus.tesapp.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class Menu extends Activity {

    public void setupMenu(){
        SlidingMenu slidingMenu = new SlidingMenu(this);
        slidingMenu.setMode(SlidingMenu.LEFT);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
        slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        slidingMenu.setFadeDegree(0.35f);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setMenu(R.layout.slide_menu);
    }
}

之后,这个滑动菜单方法“setupMenu”我在另一个类中调用,如:

public class C_News extends Activity{


   @Override
   protected void onCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);
       overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
       setContentView(R.layout.activity_home);
       TextView title = (TextView) findViewById(R.id.titlePage);
       title.setText("News & Information");
       Menu sliding = new Menu();
       sliding.setupMenu();

   }

}

但是,当我运行显示错误

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.content.res.Resources android.content.Context.getResources()'

1 个答案:

答案 0 :(得分:0)

您可以在其他活动中重复使用

public class Menu {

public static void setupMenu(Context context){
    SlidingMenu slidingMenu = new SlidingMenu(context);
    slidingMenu.setMode(SlidingMenu.LEFT);
    slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
    slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    slidingMenu.setFadeDegree(0.35f);
    slidingMenu.attachToActivity(context, SlidingMenu.SLIDING_CONTENT);
    slidingMenu.setMenu(R.layout.slide_menu);
}

}