2013-02-03 51 views
2

我想知道如果我能得到第二個(或許多這個問題)眼睛來幫助我這個。我不能爲我的生活找出造成這種情況的原因。基本上,這只是一個無線活動,可以讓你改變WiFi狀態。但是,我的代碼在getWiFi();的調用中踢出了一個nullPointerException異常。它也指向123行,但我沒有看到任何錯誤。任何人都可以看到爲什麼會崩潰?我在清單中的權限對我而言是正確的。這是來自java文件的代碼。第89行是getWifi();叫做。任何幫助將不勝感激。在Android中訪問和修改Wifi狀態

代碼WirelessManager.java

import android.app.Activity; 
    import android.content.Context; 
    import android.os.Bundle; 
    import android.view.View; 
     import android.view.View.OnClickListener; 
     import android.net.wifi.WifiInfo; 
     import android.net.wifi.WifiManager; 
     import android.widget.TextView; 
     import android.widget.ImageView; 
     import android.widget.Button; 

     public class WirelessManager extends Activity { 
    //index values to access the elements in the TextView array. 
    private final int SSTRENGTH = 0; 
    private final int WIFISTATE = 1; 
    private final int IPADD  = 2; 
    private final int MACADD = 3; 
    private final int SSID  = 4; 
    private final int LINKSPD = 5;  

    private TextView[] data_labels; 
    private TextView name_label; 
    private TextView enable_label; 
    private Button state_button; 
    private Button back_button; 
    private WifiManager wifi; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.info_layout); 

     wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
     TextView[] titles = new TextView[6]; 
     data_labels = new TextView[6]; 

     int[] left_views = {R.id.first_title, R.id.second_title, R.id.third_title, 
         R.id.fourth_title, R.id.fifth_title}; 

     /*R.layout.info_layout is the same layout used for directory info. 
     *Re-using the layout for this activity, so id tag names may not make sense, 
     *but are in the correct order. 
     */ 
     int[] right_views = {R.id.dirs_label, R.id.files_label, R.id.time_stamp, 
          R.id.total_size, R.id.free_space}; 
     String[] labels = {"Signal strength", "WIFI State", "ip address", 
          "mac address", "SSID", "link speed"}; 

     for (int i = 0; i < 5; i++) { 
      titles[i] = (TextView)findViewById(left_views[i]); 
      titles[i].setText(labels[i]); 

      data_labels[i] = (TextView)findViewById(right_views[i]); 
      data_labels[i].setText("N/A"); 
     } 

     name_label = (TextView)findViewById(R.id.name_label); 
     enable_label = (TextView)findViewById(R.id.path_label); 
     state_button = (Button)findViewById(R.id.back_button); 
     back_button = (Button)findViewById(R.id.zip_button); 
     back_button.setText(" Back "); 

     state_button.setOnClickListener(new ButtonHandler()); 
     back_button.setOnClickListener(new ButtonHandler()); 

     ImageView icon = (ImageView)findViewById(R.id.info_icon); 
     icon.setImageResource(R.drawable.wireless); 

     get_wifi(); 
    } 

    private void get_wifi() { 
     WifiInfo info = wifi.getConnectionInfo(); 
     int state = wifi.getWifiState(); 
     int strength = WifiManager.calculateSignalLevel(info.getRssi(), 5); 
     boolean enabled = wifi.isWifiEnabled(); 

     name_label.setText(info.getSSID()); 
     enable_label.setText(enabled ?"Your wifi is enabled" :"Your wifi is not enabled"); 
     state_button.setText(enabled ?"Disable wifi" : "Enable wifi"); 

     switch(state) { 
      case WifiManager.WIFI_STATE_ENABLED: 
       data_labels[WIFISTATE].setText(" Enabled"); 
       break; 
      case WifiManager.WIFI_STATE_DISABLED: 
       data_labels[WIFISTATE].setText(" Disabled"); 
       break; 
      case WifiManager.WIFI_STATE_DISABLING: 
       data_labels[WIFISTATE].setText(" Being Disabled"); 
       break; 
      case WifiManager.WIFI_STATE_ENABLING: 
       data_labels[WIFISTATE].setText(" Being Enabled"); 
       break; 
      case WifiManager.WIFI_STATE_UNKNOWN: 
       data_labels[WIFISTATE].setText(" Unknown"); 
       break; 
     } 
     if(enabled) { 
      data_labels[IPADD].setText(FileManager.integerToIPAddress(info.getIpAddress())); 
      data_labels[MACADD].setText(info.getMacAddress()); 
      data_labels[SSID].setText(info.getSSID()); 
      data_labels[LINKSPD].setText(info.getLinkSpeed() + " Mbps"); 
      data_labels[SSTRENGTH].setText("strength " + strength); 
     }else { 
      data_labels[IPADD].setText("N/A"); 
      data_labels[MACADD].setText(info.getMacAddress()); 
      data_labels[SSID].setText("N/A"); 
      data_labels[LINKSPD].setText("N/A"); 
      data_labels[SSTRENGTH].setText("N/A"); 
     } 
    } 

    private class ButtonHandler implements OnClickListener { 

     public void onClick(View v) { 

      if(v.getId() == R.id.back_button) { 
       if(wifi.isWifiEnabled()){ 
        wifi.setWifiEnabled(false); 
        state_button.setText("Enable wifi"); 
       }else { 
        wifi.setWifiEnabled(true); 
        state_button.setText("Disable wifi"); 
        get_wifi(); 
       } 
      }else if(v.getId() == R.id.zip_button) 
       finish(); 
     } 
    } 

} 


Code for AndroidManifest.xml 


     <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="coxxxxxxxxxx" 
     android:versionCode="100" 
     android:versionName="1.0.0" 
     android:installLocation="auto"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name" > 



     <activity android:name=".Settings" android:label="@string/app_name" /> 

     <activity android:name=".ProcessManager" android:label="@string/manager_act" /> 
     <activity android:name=".WirelessManager" android:label="Wireless Information" /> 
     <activity android:name=".ApplicationBackup" android:label="Installed Applications" /> 



       <activity android:name=".Main" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.intent.action.GET_CONTENT" /> 
       <data android:mimeType="*/*" /> 
       <category android:name="android.intent.category.OPENABLE" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 

     </activity> 

     <activity android:name=".AudioPlayblack" 
        android:label="@string/media_dialog" 
        android:theme="@android:style/Theme.Dialog" /> 

     <activity android:name=".DirectoryInfo" android:enabled="true" 
        android:label="@string/dir_info"> 
      <intent-filter> 
       <action android:name="android.intent.action.VIEW" /> 
       <data android:mimeType="application/manager" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

     <activity android:name=".HelpManager" 
        android:label="XXXXXXXXX" 
        android:theme="@android:style/Theme.Dialog" /> 


</application> 
      <supports-screens android:smallScreens="true" 
         android:normalScreens="true" 
         android:largeScreens="true" /> 
      <uses-sdk android:minSdkVersion="4" 
       android:maxSdkVersion="10"/> 

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

     <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 


     </manifest> 

回答

1

關閉1個錯誤。你的for循環得到的意見去我< 5,但你訪問LINKSP我= 5。增加你的循環和所有陣列1.

+0

WOW !!!謝謝你......我不知道我是如何失蹤的。你釘了它。現在工作正常。我今天至少看了一個半小時,看不到它!我不能夠感謝你! –