SimonVT menuDrawer 中出现 java.lang.NullPointerException?

标签 java android nullpointerexception togglebutton slidingdrawer

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

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

错误是:java.lang.NullPointerException

这是我的代码: 配置文件.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);

关于SimonVT menuDrawer 中出现 java.lang.NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32944015/

相关文章:

Android:从另一个启动新 Intent 的类调用方法

java - 什么是NullPointerException,我该如何解决?

java - setContentView 抛出 NullPointerException

java - 如何解决android.database.sqlite.SQLiteException : no such column: ttd (code 1)

java - 在膨胀的 LinearLayout 中通过 ID 获取元素

android - 使用两行不同的字体创建微调器下拉菜单

c# - 如何通过 C#/Xamarin 中 webview 的链接调用 whatsapp?

Java - 通过套接字发送文件(聊天客户端 -> 服务器 -> 另一个聊天客户端)

java - 找到两个线性等式成立的整数集

java - 如何在 Selenium Java Webdriver 中验证图像的尺寸?