似乎每当我在onSensorChanged中使用mWiFiManager.isWifiEnabled()时,我的应用程序崩溃了

时间:2014-03-22 14:25:03

标签: android android-sensors sensormanager

每当我尝试在设备(Nexus 5)上运行应用程序时,应用程序崩溃。

每当我尝试在SensorManager中运行时,问题似乎都出现在doThis()方法中。在大多数情况下,当我试图显示设备指向的角度时,应用程序正常工作。但是当我尝试访问wifimanager或连接管理器以检查我的wifi是否打开时,它会崩溃。更糟糕的是,我还没有能够计算出算法的其余部分。

package com.example.rss_test;

import android.os.Bundle;  
import android.app.Activity;  
import android.content.Context;  
import android.view.Menu;     

import java.util.ArrayList;
import java.util.List;

import android.net.ConnectivityManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import com.example.rss_test.sector;
import android.widget.TextView;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

public class MainActivity extends Activity implements SensorEventListener {
List<sector> mySectors = new ArrayList<sector>();
SensorManager mySM;
float angle = 0;
float angleDiff = 0;
int initialized = 0;
int x;
TextView text1i, text2i, text3i;
StringBuilder sb = new StringBuilder();
sector tempSec;
WifiManager mWiFiManager;
ScanResult sr;
boolean WiFiOn = false;
ConnectivityManager conMan;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    conMan = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    mySM = (SensorManager) getSystemService(SENSOR_SERVICE);
    mWiFiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    mySM.registerListener(this, mySM.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
    text2i = (TextView) findViewById(R.id.text2);
    text1i = (TextView) findViewById(R.id.text1);
    text3i = (TextView) findViewById(R.id.text3);
}

@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;
}
@Override
protected void onResume()
{
    super.onResume();
    //WiFiOn = mWiFiManager.isWifiEnabled();
    mySM.registerListener(this, mySM.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onPause()
{
    super.onPause();
    mySM.unregisterListener(this);
    text2i.setText("");
}



public void onSensorChanged(SensorEvent event){

    angle = event.values[0];
    text3i.setText(Float.toString(angle));
    //mySM.unregisterListener(this);
    doThis();
    //mySM.registerListener(this, mySM.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);

}


public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // not in use
}

public void doThis()
{
    if(mWiFiManager.isWifiEnabled())        //this is where the problem seems to be
    {
         /*if(initialized >0)
        {
            if((angle - angleDiff>100.0) || (angle - angleDiff<-100.0) )
            {

                tempSec = new sector(mWiFiManager.getScanResults(), angle);
                mySectors.add(tempSec);
                sb.append(Float.toString(mySectors.get(mySectors.size()-1).angle));
                sb.append("\n");
                for(x = 0; x<mySectors.get(mySectors.size()-1).SectorResults.size(); )
                {
                    sr = mySectors.get(mySectors.size()-1).SectorResults.get(x);
                    x=x+1;
                    sb.append(x + "." + "ACCESS POINT NAME: " + sr.SSID);
                    sb.append("\n");
                    sb.append("BSSID: " + sr.BSSID);
                    sb.append("\n");
                    sb.append("SIGNAL: " + sr.level);
                    sb.append("\n");
                    sb.append("\n");

                }

                text1i.setText("Success");
                angleDiff = angle;


            }


            if(initialized>1)
            {
                initialized--;
            }
        }

        else
        {
            initialized++;
            angleDiff = angle;
        }
        text1i.setText(sb.toString());
    */
    }

    else
    {
    text2i.setText("WiFi is disabled");
    } 
}


}

0 个答案:

没有答案