1

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

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

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);
        }
    }
}
4

1 回答 1

1

您将收到 NullPointer 异常,因为您的 Activitys onCreate 方法没有 setContentView(R.layout.yourlayout) 调用。像这样做:

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

        // your other stuff
    }
于 2013-08-09T14:03:18.253 回答