Caused by: java.lang.IllegalStateException: 系统服务在 onCreate() 之前对 Activity 不可用
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
我的 MainActivity 中有 "check_wifi_available" 方法,它检测以 "SDF-" 字符开头的特定 SSID。该方法在那里运行良好但是当我超出范围时我遇到了问题虽然我超出了范围但我仍然得到最后的扫描结果所以我决定从我的电话中调用它 "ConnectionBroadcast" class 但在短时间内应用程序崩溃了。
错误:
<?php
04-08 00:13:25.412: E/AndroidRun04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2567)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread.access00(ActivityThread.java:161)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.os.Handler.dispatchMessage(Handler.java:102)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.os.Looper.loop(Looper.java:157)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread.main(ActivityThread.java:5356)
04-08 00:17:48.229: E/AndroidRuntime(849): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 00:17:48.229: E/AndroidRuntime(849): at java.lang.reflect.Method.invoke(Method.java:515)
04-08 00:17:48.229: E/AndroidRuntime(849): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
04-08 00:17:48.229: E/AndroidRuntime(849): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
04-08 00:17:48.229: E/AndroidRuntime(849): at dalvik.system.NativeStart.main(Native Method)
04-08 00:17:48.229: E/AndroidRuntime(849): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.Activity.getSystemService(Activity.java:4713)
04-08 00:17:48.229: E/AndroidRuntime(849): at com.bustracker.MainActivity.check_wifi_available(MainActivity.java:82)
04-08 00:17:48.229: E/AndroidRuntime(849): at com.bustracker.ConnectionBroadcast.onReceive(ConnectionBroadcast.java:44)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2552)
04-08 00:17:48.229: E/AndroidRuntime(849): ... 10 more
?>
package com.bustracker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;
public class ConnectionBroadcast extends BroadcastReceiver {
MainActivity wifi;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
boolean isConnected = netInfo != null
&& netInfo.isConnectedOrConnecting();
if (isConnected) {
Toast.makeText(context,
"The device is connected to the internet ",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context,
"Please connect the device to the internet.",
Toast.LENGTH_SHORT).show();
}
} else if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
wifi = new MainActivity();
wifi.check_wifi_available();
}
}
}
<receiver android:name=".ConnectionBroadcast" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<intent-filter>
<action android:name="android.net.wifi.SCAN_RESULTS" />
</intent-filter>
</receiver>
广播接收器中的这段代码 incorrect.You 无法实例化 activity 对象。
wifi = new MainActivity();
wifi.check_wifi_available();
您需要将 check_wifi_available() 中的任何代码放在 onReceive 中。
我的 MainActivity 中有 "check_wifi_available" 方法,它检测以 "SDF-" 字符开头的特定 SSID。该方法在那里运行良好但是当我超出范围时我遇到了问题虽然我超出了范围但我仍然得到最后的扫描结果所以我决定从我的电话中调用它 "ConnectionBroadcast" class 但在短时间内应用程序崩溃了。
错误:
<?php
04-08 00:13:25.412: E/AndroidRun04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2567)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread.access00(ActivityThread.java:161)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.os.Handler.dispatchMessage(Handler.java:102)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.os.Looper.loop(Looper.java:157)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread.main(ActivityThread.java:5356)
04-08 00:17:48.229: E/AndroidRuntime(849): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 00:17:48.229: E/AndroidRuntime(849): at java.lang.reflect.Method.invoke(Method.java:515)
04-08 00:17:48.229: E/AndroidRuntime(849): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
04-08 00:17:48.229: E/AndroidRuntime(849): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
04-08 00:17:48.229: E/AndroidRuntime(849): at dalvik.system.NativeStart.main(Native Method)
04-08 00:17:48.229: E/AndroidRuntime(849): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.Activity.getSystemService(Activity.java:4713)
04-08 00:17:48.229: E/AndroidRuntime(849): at com.bustracker.MainActivity.check_wifi_available(MainActivity.java:82)
04-08 00:17:48.229: E/AndroidRuntime(849): at com.bustracker.ConnectionBroadcast.onReceive(ConnectionBroadcast.java:44)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2552)
04-08 00:17:48.229: E/AndroidRuntime(849): ... 10 more
?>
package com.bustracker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;
public class ConnectionBroadcast extends BroadcastReceiver {
MainActivity wifi;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
boolean isConnected = netInfo != null
&& netInfo.isConnectedOrConnecting();
if (isConnected) {
Toast.makeText(context,
"The device is connected to the internet ",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context,
"Please connect the device to the internet.",
Toast.LENGTH_SHORT).show();
}
} else if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
wifi = new MainActivity();
wifi.check_wifi_available();
}
}
}
<receiver android:name=".ConnectionBroadcast" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<intent-filter>
<action android:name="android.net.wifi.SCAN_RESULTS" />
</intent-filter>
</receiver>
广播接收器中的这段代码 incorrect.You 无法实例化 activity 对象。
wifi = new MainActivity();
wifi.check_wifi_available();
您需要将 check_wifi_available() 中的任何代码放在 onReceive 中。