试图访问另一个类中的字符串崩溃应用程序

时间:2011-10-15 00:59:06

标签: android

我正在尝试创建一个能够处理某些路由器功能的类,例如检索所连接路由器的Mac地址。我想要一个方法,可以在多个类中调用它。这是路由器类:

public class Router extends Service
{
    static Router thisclass = new Router();

    WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInf = wifiMan.getConnectionInfo();

    public String BSSID() {
        return wifiInf.getBSSID();
    }

    static String bssid() {     
        return thisclass.BSSID();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

然后在主类中我想使用这个字符串:

public class AppActivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView macAddress;

        macAddress = (TextView) findViewById(R.id.textView1);

        //Router r = new Router();
        macAddress.setText("BSSID: " + Router.bssid());
    }
}

应用程序每次都崩溃,我无法理解。我也不知道如何捕获错误,我尝试了尝试catch语句,但可以打印到logcat。你可能在这一点上知道我不太热衷于编程。最后一件事是我的ManiFest并感谢您阅读

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.infoturtle.ringbyrouter"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

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

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <activity android:name=".AppActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="Router"></service>
    </application>
</manifest>

1 个答案:

答案 0 :(得分:0)

主要问题是,当您实际没有实现服务时,您正在使用路由器扩展服务。试试这个:

public class Router {
    private WifiInfo wifiInf;

    public String bssid() {
        return wifiInf.getBSSID();  
    }

    public Router(Context context) {
        WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);  
        wifiInf = wifiMan.getConnectionInfo();
    }
}

public class AppActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView macAddress;

        macAddress = (TextView) findViewById(R.id.textView1);

        Router r = new Router(getApplicationContext());
        macAddress.setText("BSSID: " + r.bssid());
    }
}