2

我想知道我是否可以得到第二个(或很多个)眼睛来帮助我解决这个问题。我一生都无法弄清楚是什么原因造成的。基本上,这只是一个允许您更改 wifi 状态的无线活动。但是,我的代码在调用 getWiFi(); 时踢出 nullPointerException。它也指向第 123 行,但我认为没有错。谁能明白为什么这会崩溃?据我所知,我在清单中的权限是正确的。这是java文件中的代码。第 89 行是 getWifi(); 叫做。任何帮助将不胜感激。

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> 
4

1 回答 1

1

关闭 1 个错误。您获取视图的 for 循环 i<5,但您正在使用 LINKSP 访问 i=5。将您的 for 循环和所有数组增加 1。

于 2013-02-03T02:54:28.847 回答