java - "cannot convert from element type Object to WifiConfiguration" -
issue:
type mismatch: cannot convert element type object wificonfiguration line 124
issue location line 124: for( wificonfiguration : list ) {
source code:
import java.util.list; import android.net.wifi.wificonfiguration; import android.net.wifi.wifiinfo; import android.net.wifi.wifimanager; import android.nfc.ndefmessage; import android.nfc.nfcadapter; import android.os.bundle; import android.os.parcelable; import android.app.activity; import android.app.pendingintent; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.util.log; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class connect2 extends activity { private edittext wifiname; private string password; private nfcadapter madapter; private pendingintent mpendingintent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.connect2); madapter = nfcadapter.getdefaultadapter(this); // create generic pendingintent deliver activity. // nfc stack // fill in intent details of discovered tag before // delivering // activity. mpendingintent = pendingintent.getactivity(this, 0, new intent(this, getclass()).addflags(intent.flag_activity_single_top), 0); // setup intent filter mime based dispatches intentfilter ndef = new intentfilter(nfcadapter.action_ndef_discovered); wifimanager wifimgr = (wifimanager)getsystemservice(context.wifi_service); wifiinfo wifiinfo = wifimgr.getconnectioninfo(); string connectionname = wifiinfo.getssid(); textview tv=(textview)findviewbyid(r.id.wifiname); if(connectionname==null){ tv.settext("you not connected "); }else{ tv.settext("you connected to: "+connectionname+" "); } button close=(button)findviewbyid(r.id.close); close.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub finish(); system.exit(0); } }); } @override public void onresume(){ super.onresume(); if(nfcadapter.action_tag_discovered.equals(getintent().getaction())||nfcadapter.action_ndef_discovered.equals(getintent().getaction())){ processreadintent(getintent()); } } @override public void onnewintent(intent intent){ setintent(intent); } public void processreadintent(intent intent){ parcelable[] rawmsgs = intent.getparcelablearrayextra( nfcadapter.extra_ndef_messages); // 1 message sent during beam ndefmessage msg = (ndefmessage) rawmsgs[0]; // record 0 contains mime type, record 1 aar, if present log.d("msg", msg.getrecords()[0].getpayload().tostring()); byte[]payload=msg.getrecords()[0].getpayload(); string msgtext=null; try{ //get text encoding string textencoding = ((payload[0] & 0200) == 0) ? "utf-8" : "utf-16"; //get language code int languagecodelength = payload[0] & 0077; string languagecode = new string(payload, 1, languagecodelength, "us-ascii"); //get text msgtext = new string(payload, languagecodelength + 1, payload.length - languagecodelength - 1, textencoding); }catch(exception e){ } // splitting message comma. first part name , 2nd part password. string[]tagdata=msgtext.split(","); string networkssid = tagdata[0].tostring(); string networkpass = tagdata[1].tostring(); wificonfiguration conf = new wificonfiguration(); conf.ssid = "\"" + networkssid + "\""; // please note quotes. string should contain ssid in quotes conf.presharedkey = "\""+ networkpass +"\""; wifimanager wifimanager = (wifimanager)getsystemservice(context.wifi_service); wifimanager.addnetwork(conf); list list = wifimanager.getconfigurednetworks(); for( wificonfiguration : list ) { if(i.ssid != null && i.ssid.equals("\"" + networkssid + "\"")) { wifimanager.disconnect(); wifimanager.enablenetwork(i.networkid, true); wifimanager.reconnect(); break; } } textview wifiname=(textview)findviewbyid(r.id.wifiname); wifiname.settext("you connected to"+networkssid +" "); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
list list = wifimanager.getconfigurednetworks();
should be:
list<wificonfiguration> list = wifimanager.getconfigurednetworks();
see wifimanager#getconfigurednetworks()
's javadoc.
the error because of list
raw type not know type of element, assumes of type object
. when declaring list list<wificonfiguration>
, guarantee @ compile-time elements added list must of type wificonfiguration
.
Comments
Post a Comment