gpt4 book ai didi

SimonVT menuDrawer 中出现 java.lang.NullPointerException?

转载 作者:太空宇宙 更新时间:2023-11-04 13:22:34 28 4
gpt4 key购买 nike

我是一名 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com