简体   繁体   English

Android SlidingMenu nullpointer切换

[英]Android SlidingMenu nullpointer on toggle

I've implemented an SlidingMenu, I set it up with SlidingMenu sm = getSlidingMenu() in the activity I want to use the menu. 我已经实现了一个SlidingMenu,我在要使用菜单的活动中使用SlidingMenu sm = getSlidingMenu()进行了设置。 The problem I've got I want to toggle it via a button I've created on a canvas. 我遇到的问题是我想通过在画布上创建的按钮来切换它。 If I click these area on the canvas it will stop drawing and open the sliding menu - works until here. 如果我在画布上单击这些区域,它将停止绘图并打开滑动菜单-直到这里都可以使用。 But then I added some content with an linearlayout and a textview in it, on clicking the textview the text changes (kind of menu to change some options). 但是随后,我添加了一些内容,其中包含一个线性布局和一个textview,单击textview时,文本会更改(菜单种类可以更改某些选项)。 And after it changed once the next click should toggle the sliding menu again. 更改一次后,下次单击应再次切换滑动菜单。 I've created the behind view's in another class, and triggered a method in the activity containing the slidingmenu, it looks like this: 我在另一个类中创建了后视图,并在包含滑动菜单的活动中触发了一个方法,如下所示:

    public void toggleSM() {
    Thread splash = new Thread(new Runnable() {

        @SuppressWarnings("static-access")
        @Override
        public void run() {
            try {
                Thread.currentThread().sleep(500);
                getSlidingMenu().toggle(true);
                Thread.currentThread().sleep(1000);
                animateButton.setLeftState(false);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    });
    splash.start();
}

I added the sleep to avoid any problems caused by the sm animation or changing content, the aninamteButton is to trigger the drawing of the canvas again. 我添加了睡眠以避免由sm动画或更改内容引起的任何问题,aninamteButton用于再次触发画布的绘制。 But it throws a nullpointer exception. 但是它抛出了一个空指针异常。 With and without animation of the slidingmenu. 有和没有动画的滑动菜单。

EDIT: 编辑:

The two classes I'm using: 我正在使用的两个类:

public class Ingame extends SlidingActivity implements OnTouchListener{

private LeftMenu lm;
private SlidingMenu sm;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sh = new ScreenHandler(this);
    fh = new FilesHandler(this);
    animateMap = new AnimateMap(this);
    ch = new CoordsHandler(this);
    animateButton = new AnimateButton();
    sched = new Schedule();
    lm = new LeftMenu(this);

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);    
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setupViews();

    lm.setupLeftMenu();
    setBehindContentView(lm.getLayout());
    setupSlidingMenu();
}

@Override
protected void onPause() {
    super.onPause();
    gb.pause();
}
@Override
protected void onResume() {
    super.onResume();
    gb.resume();
}

@SuppressWarnings("deprecation")
public void setupViews() {
    FrameLayout fl = new FrameLayout(getBaseContext());
    LayoutParams sizeP = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    fl.setLayoutParams(sizeP);
    fl.setBackgroundColor(Color.rgb(0, 153, 204));

    gb = new GameBoard(getBaseContext());
    LayoutParams fillP = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    gb.setLayoutParams(fillP);      

    gb.setOnTouchListener(Ingame.this); 
    fl.addView(gb);
    setContentView(fl); 
}

public void setupSlidingMenu() {
    sm = getSlidingMenu();
    sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
    sm.setMenu(lm.getLayout());
}

@SuppressWarnings("static-access")
@Override
public boolean onTouch(View v, MotionEvent e) {
    float distX, distY;
    switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            startX = e.getX();
            startY = e.getY();
            gb.movedDist.put("startX", startX);
            gb.movedDist.put("startY", startY);
            finX = finY = -1;
            gb.movedDist.put("finX", finX);
            gb.movedDist.put("finY", finY);
            break;
        case MotionEvent.ACTION_MOVE:
            finX = e.getX();
            finY = e.getY();
            distX = startX - finX;
            distY = startY - finY;
            if(!checkUnvalidValues() && (Math.abs(distX) > sh.getTSize() || Math.abs(distY) > sh.getTSize())) {
                int rectDistX = (int) (distX/sh.getTSize());
                int rectDistY = (int) (distY/sh.getTSize());
                noMove = false;
                if(!checkEdges(rectDistX, rectDistY)) {
                    animateMap.animateXYDistance(rectDistX, rectDistY);
                    startX = e.getX();
                    startY = e.getY();
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            finX = e.getX();
            finY = e.getY();
            if(checkButton() == IngameButton.PP) {
                animateButton.changePPState();      
                break;
            }
            if(checkButton() == IngameButton.LEFT) { //WHERE I TRIGGER THE SLIDINGMENU
                animateButton.setLeftState(true);
                if(!animateButton.getPPState())
                    animateButton.setPPState(true);
                sm.toggle(true);
                lm.openLeftMenu();
                break;
            }

            if(noMove && !checkUnvalidValues())
                animateMap.tellCoordinate(finX, finY);
            noMove = true;
            break;
    }
    return true;
}

private boolean checkUnvalidValues() {
    if(startX <= sh.getSideSize()-1 || startX >= (sh.getScreenWidth()-sh.getSideSize()))
        return true;
    if(startY <= -1 || startY >= (sh.getScreenHeight()-sh.getBottomGUISize()))
        return true;
    if(finX <= sh.getSideSize()-1 || finX >= (sh.getScreenWidth()-sh.getSideSize()))
        return true;
    if(finY <= -1 || finY >= (sh.getScreenHeight()-sh.getBottomGUISize()))
        return true;
    return false;
}

public SlidingMenu getSM() {
    return sm;
}

public void toggleSM() {

    runOnUiThread(new Runnable() {

        @SuppressWarnings("static-access")
        @Override
        public void run(){

                getSlidingMenu().toggle(true);
                SlidingMenu sm = getSlidingMenu();
                sm.getAlpha();
            try {
                Thread.currentThread().sleep(1000);
                animateButton.setLeftState(false);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
         }
     });
}

LeftMenu: LeftMenu:

public class LeftMenu {

private Context context;
private Ingame ingame;
private AnimateButton animateButton;

private LinearLayout ll;
private LinearLayout.LayoutParams llp;
private static LeftMenuPage page;
private static int buttonTextSize = 15;

public LeftMenu(Context context) {
    this.context = context;
    ingame = new Ingame();  
    animateButton = new AnimateButton();
}

public void setupLeftMenu() {
    page = LeftMenuPage.OVERVIEW;
    setupLinearLayout();
    setupButtons();
}

@SuppressWarnings("deprecation")
private void setupLinearLayout() {
    ll = new LinearLayout(context);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setBackgroundColor(0xff000000);
    llp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.FILL_PARENT,
        LinearLayout.LayoutParams.FILL_PARENT);
    ll.setLayoutParams(llp);
}
public LinearLayout getLayout() {
    return ll;
}

private void setupButtons() {
    final TextView t1 = new TextView(context);
    t1.setBackgroundColor(0xff000000);
    t1.setTextColor(0xffffffff);
    t1.setTypeface(null, Typeface.NORMAL);
    t1.setTextSize(buttonTextSize);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.weight = 1.0f;
    lp.gravity=1;
    t1.setLayoutParams(lp);

    String content = null;
    if(page == LeftMenuPage.OVERVIEW) {
        content = "Creation Mode";
    }
    else {
        content = "Back";
    }
    t1.setText(content);
    ll.addView(t1);

    t1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t1.setTextColor(0xffcccccc);
            switch(page) {
                case OVERVIEW:      
                    page = LeftMenuPage.CREATE;
                    t1.setText("Create Dwarves");

                    ingame.toggleSM();
                    if(!animateButton.getPPState())
                        animateButton.setPPState(true);

                    break;
                case CREATE:
                    ingame.toggleSM();
                    if(!animateButton.getPPState())
                        animateButton.setPPState(true);
                    break;
            }
            t1.setTextColor(0xffffffff);
        }
    });
}

public void openLeftMenu() {
    page = LeftMenuPage.OVERVIEW;
}

} }

EDIT 2: 编辑2:

    05-10 15:14:53.535: E/AndroidRuntime(25064): FATAL EXCEPTION: main
05-10 15:14:53.535: E/AndroidRuntime(25064): java.lang.NullPointerException
05-10 15:14:53.535: E/AndroidRuntime(25064):    at com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity.getSlidingMenu(SlidingActivity.java:104)
05-10 15:14:53.535: E/AndroidRuntime(25064):    at me.G4meM0ment.DwarvenSkill.Ingame$1.run(Ingame.java:234)
05-10 15:14:53.535: E/AndroidRuntime(25064):    at android.os.Handler.handleCallback(Handler.java:615)
05-10 15:14:53.535: E/AndroidRuntime(25064):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 15:14:53.535: E/AndroidRuntime(25064):    at android.os.Looper.loop(Looper.java:137)
05-10 15:14:53.535: E/AndroidRuntime(25064):    at android.app.ActivityThread.main(ActivityThread.java:4918)
05-10 15:14:53.535: E/AndroidRuntime(25064):    at java.lang.reflect.Method.invokeNative(Native Method)
05-10 15:14:53.535: E/AndroidRuntime(25064):    at java.lang.reflect.Method.invoke(Method.java:511)
05-10 15:14:53.535: E/AndroidRuntime(25064):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
05-10 15:14:53.535: E/AndroidRuntime(25064):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
05-10 15:14:53.535: E/AndroidRuntime(25064):    at dalvik.system.NativeStart.main(Native Method)

StackTrace, as I already mentionen in some comments, getSlidingMenu returns null and mHelper (SlidingActivityHelper) causes this because it's null 正如我在一些评论中已经提到的,StackTrace,getSlidingMenu返回null,而mHelper(SlidingActivityHelper)导致此问题,因为它为null

getSlidingMenu().toggle(true);

has to run on the UI Thread. 必须在UI线程上运行。 Move it inside and Handler or if your are inside an Activity use 将其移至Handler内,或者将其移至Activity内

 runOnUiThread(new Runnable() {

     public void run(){
          getSlidingMenu().toggle(true);
      }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM