单击切换按钮时出现 NullpointerException

发布于 2025-01-07 08:17:15 字数 2805 浏览 5 评论 0原文

我正在尝试让 ToggleButton 正常工作,但遇到了一些问题。 1. 单击它并导航到另一个屏幕时,它似乎恢复为默认值。 2. 我尝试使用 SharedPreferences 将其 isChecked() 值传输到另一个活动,但现在崩溃了。

    <ToggleButton
    android:id="@+id/togBTN"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/homeBtn"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="106dp"
    android:text="Voice Alerts"
    android:textOff="OFF"
    android:textOn="ON" />

上面的代码在我的xml文件中。 下面的代码是它的 onClickListener 代码。

         case R.id.togBTN:
         SharedPreferences.Editor editor = preferences.edit();
         editor.putBoolean("tgpref", tg.isChecked()); // value to store
         editor.commit();
     break;
     }  

这段代码是我尝试检索值的地方,

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    boolean tgpref = preferences.getBoolean("tgpref", true);  //default is true
    if (tgpref = true) //if (tgpref) may be enough, not sure
    {
      speakValue = true;
    }
    else
    {
     speakValue = false;
    }

这就是我在 Logcat 中得到的:

    02-21 14:49:09.587: W/dalvikvm(483): threadid=1: thread exiting with uncaught exception     (group=0x40015560)
02-21 14:49:09.597: E/AndroidRuntime(483): FATAL EXCEPTION: main
02-21 14:49:09.597: E/AndroidRuntime(483): java.lang.NullPointerException
02-21 14:49:09.597: E/AndroidRuntime(483):  at ohalleran.stephanie.Traffic.SettingsActivity.onClick(SettingsActivity.java:45)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.view.View.performClick(View.java:2485)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.widget.CompoundButton.performClick(CompoundButton.java:99)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.view.View$PerformClick.run(View.java:9080)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.os.Handler.handleCallback(Handler.java:587)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.os.Looper.loop(Looper.java:130)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-21 14:49:09.597: E/AndroidRuntime(483):  at java.lang.reflect.Method.invokeNative(Native Method)
02-21 14:49:09.597: E/AndroidRuntime(483):  at java.lang.reflect.Method.invoke(Method.java:507)
02-21 14:49:09.597: E/AndroidRuntime(483):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-21 14:49:09.597: E/AndroidRuntime(483):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-21 14:49:09.597: E/AndroidRuntime(483):  at dalvik.system.NativeStart.main(Native Method)

非常感谢对此的一些帮助。

I am trying to get a ToggleButton working but am having a few problems.
1. Its seems to revert back to the default value when it is clicked and I navigate to another screen.
2. I tried transferring its isChecked() value to another activity using SharedPreferences and it now crashes.

    <ToggleButton
    android:id="@+id/togBTN"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/homeBtn"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="106dp"
    android:text="Voice Alerts"
    android:textOff="OFF"
    android:textOn="ON" />

The above code is in my xml file.
The code below is the onClickListener code for it.

         case R.id.togBTN:
         SharedPreferences.Editor editor = preferences.edit();
         editor.putBoolean("tgpref", tg.isChecked()); // value to store
         editor.commit();
     break;
     }  

And this code is where I try to retrieve the value

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    boolean tgpref = preferences.getBoolean("tgpref", true);  //default is true
    if (tgpref = true) //if (tgpref) may be enough, not sure
    {
      speakValue = true;
    }
    else
    {
     speakValue = false;
    }

This is what I am getting in the Logcat:

    02-21 14:49:09.587: W/dalvikvm(483): threadid=1: thread exiting with uncaught exception     (group=0x40015560)
02-21 14:49:09.597: E/AndroidRuntime(483): FATAL EXCEPTION: main
02-21 14:49:09.597: E/AndroidRuntime(483): java.lang.NullPointerException
02-21 14:49:09.597: E/AndroidRuntime(483):  at ohalleran.stephanie.Traffic.SettingsActivity.onClick(SettingsActivity.java:45)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.view.View.performClick(View.java:2485)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.widget.CompoundButton.performClick(CompoundButton.java:99)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.view.View$PerformClick.run(View.java:9080)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.os.Handler.handleCallback(Handler.java:587)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.os.Looper.loop(Looper.java:130)
02-21 14:49:09.597: E/AndroidRuntime(483):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-21 14:49:09.597: E/AndroidRuntime(483):  at java.lang.reflect.Method.invokeNative(Native Method)
02-21 14:49:09.597: E/AndroidRuntime(483):  at java.lang.reflect.Method.invoke(Method.java:507)
02-21 14:49:09.597: E/AndroidRuntime(483):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-21 14:49:09.597: E/AndroidRuntime(483):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-21 14:49:09.597: E/AndroidRuntime(483):  at dalvik.system.NativeStart.main(Native Method)

Would really appreciate some help with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光匆匆的小流年 2025-01-14 08:17:15

您应该使用 == 运算符 而不是 =

You should use == operator instead of =.

落叶缤纷 2025-01-14 08:17:15

如果 (tgpref) 足够,

形式的语句

当您有ABCDE

或者 A 为空时, 就会发生空指针异常
或 AB 为空
或 ABC 为空
或 ABCD 为 null

点左侧的某些内容为 null。

检查SettingsActivity.java的第45行,你会在那里找到罪魁祸首。

if (tgpref) is enough

a null pointer exception occurs when you have a statement of the form

A.B.C.D.E

either A is null
or A.B is null
or A.B.C is null
or A.B.C.D is null

something at the left of a dot is null.

Check line 45 of SettingsActivity.java, you will find your culprit there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文