I created a simple App to scan and list Wifi Connections around a device but when running on Samsung Galaxy S4, the app says Unfortunately 'app name' has stopped working. Should I be changing any permissions on the mobile? My Wifi on mobile is ON and the app has permissions to access and view Wifi connections. The app compiles without errors on eclipse.
I am using the wifi code mentioned over here: Android Wi-Fi Tutorial
Here is the logcat exception:
*06-27 02:10:15.891: E/memtrack(22391): Couldn't load memtrack module (No such file or directory)
06-27 02:10:15.891: E/android.os.Debug(22391): failed to load memtrack module: -2
06-27 02:10:16.026: E/EnterpriseContainerManager(3031): ContainerPolicy Service is not yet
ready!!!
06-27 02:10:16.341: E/EnterpriseContainerManager(3031): ContainerPolicy Service is not yet ready!!!
06-27 02:10:18.401: E/AndroidRuntime(22277): FATAL EXCEPTION: main
06-27 02:10:18.401: E/AndroidRuntime(22277): Process: com.example.wifi, PID: 22277
06-27 02:10:18.401: E/AndroidRuntime(22277): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x4000010 } in com.example.wifi.MainActivity$WifiScanReceiver@42a3b950
06-27 02:10:18.401: E/AndroidRuntime(22277): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:782)
06-27 02:10:18.401: E/AndroidRuntime(22277): at android.os.Handler.handleCallback(Handler.java:733)
06-27 02:10:18.401: E/AndroidRuntime(22277): at android.os.Handler.dispatchMessage(Handler.java:95)
06-27 02:10:18.401: E/AndroidRuntime(22277): at android.os.Looper.loop(Looper.java:157)
06-27 02:10:18.401: E/AndroidRuntime(22277): at android.app.ActivityThread.main(ActivityThread.java:5356)
06-27 02:10:18.401: E/AndroidRuntime(22277): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 02:10:18.401: E/AndroidRuntime(22277): at java.lang.reflect.Method.invoke(Method.java:515)
06-27 02:10:18.401: E/AndroidRuntime(22277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-27 02:10:18.401: E/AndroidRuntime(22277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-27 02:10:18.401: E/AndroidRuntime(22277): at dalvik.system.NativeStart.main(Native Method)
06-27 02:10:18.401: E/AndroidRuntime(22277): Caused by: java.lang.NullPointerException
06-27 02:10:18.401: E/AndroidRuntime(22277): at com.example.wifi.MainActivity$WifiScanReceiver.onReceive(MainActivity.java:54)
06-27 02:10:18.401: E/AndroidRuntime(22277): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:772)
06-27 02:10:18.401: E/AndroidRuntime(22277): ... 9 more*
Here is the MainActivity.java code:
package com.example.wifi;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
WifiManager mainWifiObj;
WifiScanReceiver wifiReciever;
ListView list;
String wifis[];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.listView1);
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
mainWifiObj.startScan();
}
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
wifis = new String[wifiScanList.size()];
for(int i = 0; i < wifiScanList.size(); i++){
wifis[i] = ((wifiScanList.get(i)).toString());
}
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,wifis));
}
}
}
06-27 02:10:18.401: E/AndroidRuntime(22277): Caused by: java.lang.NullPointerException
06-27 02:10:18.401: E/AndroidRuntime(22277): at com.example.wifi.MainActivity$WifiScanReceiver.onReceive(MainActivity.java:54)
You have a NullPointerException
at line 54, you need to fix this before you will be able to run your app