Note that there are some explanatory texts on larger screens.

plurals
  1. PONullpointerexception when togglebutton is clicked
    primarykey
    data
    text
    <p>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. </p> <pre><code> &lt;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" /&gt; </code></pre> <p>The above code is in my xml file. The code below is the onClickListener code for it.</p> <pre><code> case R.id.togBTN: SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("tgpref", tg.isChecked()); // value to store editor.commit(); break; } </code></pre> <p>And this code is where I try to retrieve the value</p> <pre><code>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; } </code></pre> <p>This is what I am getting in the <code>Logcat</code>:</p> <pre><code> 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) </code></pre> <p>Would really appreciate some help with this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload