Android:实现滑动菜单时遇到麻烦

时间:2014-03-20 08:26:06

标签: android onclick slidingmenu

我正在尝试使用从here下载的 jfeinstein10 库实现滑动菜单。

所以我使用了以下代码。

SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.RIGHT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.activity_menu);

当我直接在onCreate方法中使用此代码时,没有问题,但是当我将此代码放在按钮的点击事件中时,如下所示。

Button mButton = (Button) findViewById(R.id.slidingMenu);
        mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                try {
                    menu.setMode(SlidingMenu.RIGHT);
                    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                    menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
                    menu.setFadeDegree(0.35f);
                    menu.attachToActivity(MainActivity.this,
                            SlidingMenu.SLIDING_CONTENT);
                    menu.setMenu(R.layout.activity_menu);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

突然间我发现了以下错误。

03-20 04:19:12.246: E/AndroidRuntime(1814): FATAL EXCEPTION: main
03-20 04:19:12.246: E/AndroidRuntime(1814): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.slidingtest/com.example.slidingtest.MainActivity}: java.lang.NullPointerException
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.os.Looper.loop(Looper.java:137)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at java.lang.reflect.Method.invokeNative(Native Method)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at java.lang.reflect.Method.invoke(Method.java:525)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at dalvik.system.NativeStart.main(Native Method)
03-20 04:19:12.246: E/AndroidRuntime(1814): Caused by: java.lang.NullPointerException
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.view.View.<init>(View.java:3254)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.view.View.<init>(View.java:3315)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.view.ViewGroup.<init>(ViewGroup.java:459)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.widget.RelativeLayout.<init>(RelativeLayout.java:248)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.<init>(SlidingMenu.java:217)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.<init>(SlidingMenu.java:203)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.<init>(SlidingMenu.java:178)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at com.example.slidingtest.MainActivity.<init>(MainActivity.java:14)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at java.lang.Class.newInstanceImpl(Native Method)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at java.lang.Class.newInstance(Class.java:1130)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
03-20 04:19:12.246: E/AndroidRuntime(1814):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
03-20 04:19:12.246: E/AndroidRuntime(1814):     ... 11 more

知道这里造成了什么。

修改

public class MainActivity extends Activity {

    SlidingMenu menu = new SlidingMenu(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/slidingMenu"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Click Me !" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

SlidingMenu menu = new SlidingMenu(this);

必须进入onCreate

创建活动后可以使用上下文。

看看

  

引起:java.lang.NullPointerException 03-20 04:19:12.246:   E / AndroidRuntime(1814):at   android.content.ContextWrapper.getResources(ContextWrapper.java:89)   03-20 04:19:12.246:E / AndroidRuntime(1814):at   android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)   03-20 04:19:12.246:E / AndroidRuntime(1814):at   android.view.View。(View.java:3254)