“无法从元素类型对象转换为WifiConfiguration”

时间:2013-04-05 16:01:37

标签: java android wifi nfc

ISSUE:

类型不匹配:无法从元素类型对象转换为WifiConfiguration第124行

问题位置第124行:for( WifiConfiguration i : list ) {

消息来源代码:

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 a generic PendingIntent that will be deliver to this activity.
        // The NFC stack
        // will fill in the intent with the details of the discovered tag before
        // delivering to
        // this activity.
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        // Setup an intent filter for all 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 are not connected  ");
        }else{

            tv.setText("You are 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);

        // only one message sent during the beam
        NdefMessage msg = (NdefMessage) rawMsgs[0];

        // record 0 contains the MIME type, record 1 is the AAR, if present
        Log.d("msg", msg.getRecords()[0].getPayload().toString());

        byte[]payload=msg.getRecords()[0].getPayload();
        String msgtext=null;
        try{

        //Get the Text Encoding
        String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";

        //Get the Language Code
        int languageCodeLength = payload[0] & 0077;
        String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");

        //Get the Text
        msgtext = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
        }catch(Exception e){

        }

        // splitting the message by comma. The first part is the name and 2nd part is the password.
        String[]tagdata=msgtext.split(",");

        String networkSSID = tagdata[0].toString();
        String networkPass = tagdata[1].toString();

        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";   // Please note the 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 i : 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 are now connected to"+networkSSID +"   ");

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}   

1 个答案:

答案 0 :(得分:2)

List list = wifiManager.getConfiguredNetworks();

应该是:

List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();

请参阅WifiManager#getConfiguredNetworks()'s JavaDoc


错误是因为List是一个原始类型,它不知道其元素的类型,所以它假设它是Object类型。将列表声明为List<WifiConfiguration>时,这将保证在编译时将任何元素添加到列表中的类型必须为WifiConfiguration