我是一个Android开发人员,正在构建一个应用程序。

我在应用中使用SimonVT menuDrawer,并且尝试添加切换按钮
在menuDrawer中,但出现错误。

错误是:java.lang.NullPointerException

这是我的代码:
Profile.java

        mDrawer = MenuDrawer.attach(this, Position.RIGHT);
        mDrawer.setContentView(R.layout.pro);
        mDrawer.setMenuView(R.layout.slide_menu);
        mDrawer.setMenuSize(600);

        toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
        registerNewAccount = (RelativeLayout) findViewById(R.id.registerNewAccount);

       registerNewAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Digits.authenticate(digitsCallback, android.R.style.Theme_Material);

            }
        });

//below line i'm getting an error.
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                // text.setText("Status: " + isChecked);
            }
        });


这是我的xml代码:
slide_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#383838"
    >

    <RelativeLayout
        android:id="@+id/layout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view1"
        android:background="#323232"
        >
     <RelativeLayout
        android:id="@+id/onOffView"
        android:layout_width="match_parent"
        android:layout_height="90dp"
         >

         <ToggleButton
             android:id="@+id/toggleButton1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerHorizontal="true"
             android:layout_centerVertical="true"
             android:background="@drawable/toggle_selector"
             android:checked="false"
             android:text=""
             android:textOff=""
             android:textOn="" />



     </RelativeLayout>

        <RelativeLayout
            android:id="@+id/registerNewAccount"
            android:layout_below="@+id/onOffView"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Register New Account"
                android:textColor="#ffffff"
                android:textSize="20dp"
                android:layout_marginTop="25dp"
                android:layout_marginLeft="15dp"
                />
        </RelativeLayout>


这是日志:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rozgari.techequity.rozgari/com.rozgari.techequity.rozgari.Profile}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)
            at android.app.ActivityThread.access$800(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5111)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.rozgari.techequity.rozgari.Profile.slideMenu(Profile.java:864)
            at com.rozgari.techequity.rozgari.Profile.onCreate(Profile.java:108)
            at android.app.Activity.performCreate(Activity.java:5248)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)
            at android.app.ActivityThread.access$800(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5111)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)


感谢帮助!

最佳答案

更换

toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
registerNewAccount = (RelativeLayout) findViewById(R.id.registerNewAccount);




 View menuLayout = LayoutInflater.from(this).inflate(R.layout.slide_menu,null);
 toggleButton = (ToggleButton) menuLayout.findViewById(R.id.toggleButton1);
 registerNewAccount = (RelativeLayout) menuLayout.findViewById(R.id.registerNewAccount);

07-26 09:42