菜单滑动错误 - android

时间:2013-08-09 13:25:08

标签: android menu sliding

我试图让菜单滑动,但我不能。

我的应用程序返回此错误:

08-09 13:19:35.661: E/AndroidRuntime(1468): FATAL EXCEPTION: main
08-09 13:19:35.661: E/AndroidRuntime(1468): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.menusliderteste/com.example.menusliderteste.MainActivity}: java.lang.NullPointerException
08-09 13:19:35.661: E/AndroidRuntime(1468):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at android.os.Looper.loop(Looper.java:126)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at android.app.ActivityThread.main(ActivityThread.java:3997)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at java.lang.reflect.Method.invokeNative(Native Method)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at java.lang.reflect.Method.invoke(Method.java:491)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at dalvik.system.NativeStart.main(Native Method)
08-09 13:19:35.661: E/AndroidRuntime(1468): Caused by: java.lang.NullPointerException
08-09 13:19:35.661: E/AndroidRuntime(1468):     at android.view.ViewGroup.addView(ViewGroup.java:2774)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at android.view.ViewGroup.addView(ViewGroup.java:2761)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at com.jeremyfeinstein.slidingmenu.lib.CustomViewAbove.setContent(CustomViewAbove.java:436)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.setContent(SlidingMenu.java:356)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.attachToActivity(SlidingMenu.java:332)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.attachToActivity(SlidingMenu.java:292)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at com.example.menusliderteste.MainActivity.onCreate(MainActivity.java:25)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-09 13:19:35.661: E/AndroidRuntime(1468):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
08-09 13:19:35.661: E/AndroidRuntime(1468):     ... 11 more

这是我的活动:

enter code here

public class MainActivity extends Activity {
    private SlidingMenu slidingMenu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        slidingMenu = new SlidingMenu(this);
        slidingMenu.setMode(SlidingMenu.LEFT);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
        slidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
        slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        slidingMenu.setFadeDegree(0.35f);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setMenu(R.layout.slidingmenu);

        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onBackPressed() {
        if ( slidingMenu.isMenuShowing()) {
            slidingMenu.toggle();
        }
        else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ( keyCode == KeyEvent.KEYCODE_MENU ) {
            this.slidingMenu.toggle();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            this.slidingMenu.toggle();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你得到一个NullPointer异常,因为你的Activity onCreate方法没有setContentView(R.layout.yourlayout)调用。 这样做:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourlayout)

        // your other stuff
    }