我已经实现了SlidingMenu,我在我要使用菜单的活动中使用SlidingMenu sm = getSlidingMenu()
进行了设置。我遇到的问题我想通过我在画布上创建的按钮来切换它。如果我在画布上单击这些区域,它将停止绘制并打开滑动菜单 - 直到这里工作。但后来我添加了一些内容,其中包含了linearlayout和textview,在单击文本视图时文本发生了变化(更改某些选项的菜单类型)。在更改后,下次单击后应再次切换滑动菜单。
我在另一个类中创建了后面的视图,并在包含滑动菜单的活动中触发了一个方法,它看起来像这样:
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();
}
我添加了睡眠以避免由sm动画或更改内容引起的任何问题,aninamteButton将再次触发画布的绘制。但它会抛出一个nullpointer异常。有和没有动画的滑动菜单。
编辑:
我正在使用的两个类:
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:
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;
}
}
编辑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,正如我在一些评论中已经提到的那样,getSlidingMenu返回null并且mHelper(SlidingActivityHelper)导致这个因为它是null
答案 0 :(得分:0)
getSlidingMenu().toggle(true);
必须在UI线程上运行。将其移到内部和处理程序中,或者如果您在活动中使用
runOnUiThread(new Runnable() {
public void run(){
getSlidingMenu().toggle(true);
}
});