I have a problem on my if
statement.
Here is my code.
public class WiFiService extends BroadcastReceiver {
Context mcontext;
SharedPreferences prefs = null;
Boolean flagIsConnected = false;
String homeBSSID = "null";
String bssid = "null";
@Override
public void onReceive(Context mcontext, Intent intent) {
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if(info != null) {
if(info.isConnected() && !flagIsConnected) {
//state change from disconnected to connected
flagIsConnected = true; // set flag here to
WifiManager wifiManager = (WifiManager)mcontext.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
Log.d("WifiConnection", "Connected");
this.mcontext = mcontext;
Wifi();
}else if(!info.isConnected() && flagIsConnected){
//state change from connect to disconnected
flagIsConnected = false;
}
}
}
private void Wifi() {
WifiManager wifiManager = (WifiManager)mcontext.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
prefs = mcontext.getSharedPreferences("com.bedrock.live", Context.MODE_PRIVATE);
bssid = wifiInfo.getBSSID();
homeBSSID = prefs.getString("homeBSSID", "null");
if (bssid.equals(homeBSSID) == true){
Log.d("WifiConnection", "Home.");
}
else
Log.d("WifiConnection", "Null.");
Log.d("WifiConnection", "HomeWifi");
}
}
when I run this and turn off the wifi, it crashes and shows me this;
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: FATAL EXCEPTION: main
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: Process: com.bedrock.live, PID: 3127
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: java.lang.RuntimeException: Unable to start receiver com.bedrock.live.service.WiFiService: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:2601)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at android.app.ActivityThread.access$1800(ActivityThread.java:144)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5238)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at com.bedrock.live.service.WiFiService.Wifi(WiFiService.java:52)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at com.bedrock.live.service.WiFiService.onReceive(WiFiService.java:35)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:2594)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at android.app.ActivityThread.access$1800(ActivityThread.java:144)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5238)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
11-02 18:16:43.269 3127-3127/com.bedrock.live E/AndroidRuntime: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
as you can see, my code is supposed to get saved string from SharedPreferences
and log "home" when it matches, and log "null" when it doesnt matches.
I have no ideas.. Please help.
Thanks.