SimonVT menuDrawer中的java.lang.NullPointerException?

时间:2015-10-05 08:17:00

标签: java android nullpointerexception togglebutton slidingdrawer

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

我在我的应用程序中使用SimonVT menuDrawer,我正在尝试添加切换按钮 在menuDrawer中但收到错误。

错误是:java.lang.NullPointerException

这是我的代码: Profile.java

<?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>

这是我的xml代码: slide_menu.xml

 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)

这是log:

tuple(next(it) for it in its)

非常感谢帮助!

1 个答案:

答案 0 :(得分:0)

替换

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