wifi - Android WifiManeger Class startCustomizedScan(ScanSettings requested) function application force stop -
i must use startcustomizedscan(scansettings requested) function. request scan access points in specified channel list. each channel specified frequency in mhz, e.g. "2437"
wifichannel mrtchannel; mrtchannel = new wifichannel(); mrtchannel.freqmhz = 2437; mrtchannel.channelnum = 6; scansettings set ; mrtcollection = null; mrtcollection.add(mrtchannel); set = (scansettings)mrtcollection; mainwifi.startcustomizedscan(set);
but application force stop. application not laucnh. logcat
08-30 23:09:36.547: e/androidruntime(14327): fatal exception: main 08-30 23:09:36.547: e/androidruntime(14327): process: com.muratucan.murat5hidden, pid: 14327 08-30 23:09:36.547: e/androidruntime(14327): java.lang.runtimeexception: unable start activity componentinfo{com.muratucan.murat5hidden/com.muratucan.murat5hidden.mainactivity}: java.lang.nullpointerexception: attempt invoke interface method 'boolean java.util.collection.add(java.lang.object)' on null object reference 08-30 23:09:36.547: e/androidruntime(14327): @ android.app.activitythread.performlaunchactivity(activitythread.java:2305) 08-30 23:09:36.547: e/androidruntime(14327): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2367) 08-30 23:09:36.547: e/androidruntime(14327): @ android.app.activitythread.access$800(activitythread.java:148) 08-30 23:09:36.547: e/androidruntime(14327): @ android.app.activitythread$h.handlemessage(activitythread.java:1283) 08-30 23:09:36.547: e/androidruntime(14327): @ android.os.handler.dispatchmessage(handler.java:102) 08-30 23:09:36.547: e/androidruntime(14327): @ android.os.looper.loop(looper.java:135) 08-30 23:09:36.547: e/androidruntime(14327): @ android.app.activitythread.main(activitythread.java:5274) 08-30 23:09:36.547: e/androidruntime(14327): @ java.lang.reflect.method.invoke(native method) 08-30 23:09:36.547: e/androidruntime(14327): @ java.lang.reflect.method.invoke(method.java:372) 08-30 23:09:36.547: e/androidruntime(14327): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:909) 08-30 23:09:36.547: e/androidruntime(14327): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:704) 08-30 23:09:36.547: e/androidruntime(14327): caused by: java.lang.nullpointerexception: attempt invoke interface method 'boolean java.util.collection.add(java.lang.object)' on null object reference 08-30 23:09:36.547: e/androidruntime(14327): @ com.muratucan.murat5hidden.mainactivity.oncreate(mainactivity.java:133) 08-30 23:09:36.547: e/androidruntime(14327): @ android.app.activity.performcreate(activity.java:5977) 08-30 23:09:36.547: e/androidruntime(14327): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) 08-30 23:09:36.547: e/androidruntime(14327): @ android.app.activitythread.performlaunchactivity(activitythread.java:2258)
please give me insight in project or other method.
thanks!
my full code here..
public class mainactivity extends activity { // wifimanager mainwifi; public collection<wifichannel> channelsetcollect; public collection<wifichannel> mrtcollection = null; textview ekran ; textview maintext; wifimanager mainwifi; wifireceiver receiverwifi; list<scanresult> wifilist; stringbuilder sb = new stringbuilder(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); maintext = (textview) findviewbyid(r.id.textview1); // initiate wifi service manager mainwifi = (wifimanager) getsystemservice(context.wifi_service); wifichannel mrtchannel; mrtchannel = new wifichannel(); mrtchannel.freqmhz = 2437; mrtchannel.channelnum = 6; scansettings set ; mrtcollection = null; mrtcollection.add(mrtchannel); set = (scansettings)mrtcollection; mainwifi.startcustomizedscan(set); // check wifi disabled if (mainwifi.iswifienabled() == false) { // if wifi disabled enable toast.maketext(getapplicationcontext(), "wifi disabled..making enabled", toast.length_long).show(); mainwifi.setwifienabled(true); } // wifi scaned value broadcast receiver receiverwifi = new wifireceiver(); // register broadcast receiver // broacast receiver automatically call when number of wifi connections changed registerreceiver(receiverwifi, new intentfilter(wifimanager.scan_results_available_action)); // mainwifi.startscan(); maintext.settext("starting scan..."); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } protected void onpause() { unregisterreceiver(receiverwifi); super.onpause(); } protected void onresume() { registerreceiver(receiverwifi, new intentfilter(wifimanager.scan_results_available_action)); super.onresume(); } // broadcast receiver class called receive method // when number of wifi connections changed class wifireceiver extends broadcastreceiver { // method call when number of wifi connections changed public void onreceive(context c, intent intent) { sb = new stringbuilder(); wifilist = mainwifi.getscanresults(); sb.append("\n number of wifi connections :"+wifilist.size()+"\n\n"); for(int = 0; < wifilist.size(); i++){ sb.append(new integer(i+1).tostring() + ". "); sb.append((wifilist.get(i)).tostring()); sb.append("\n\n"); } maintext.settext(sb); } } }
you getting nullpointerexception because trying add collection, collection null.
mrtcollection = null; mrtcollection.add(mrtchannel);
what need initialize collection:
mrtcollection = new arraylist<wifichannel>(); mrtcollection.add(mrtchannel);
note, can use class implements collection, not arraylist (see collection see classes implement collection). also, can initialize mrtcollection when define variable, or can assign in oncreate.
update: classcastexception, shouldn't casting arraylist scansettings, should creating own scansettings using hidden constructor , setting collection channelset desired channels.
Comments
Post a Comment