原因:java.lang.ClassCastException:com.bustracker.MainActivity 无法转换为 android.content.BroadcastReceiver

Caused by: java.lang.ClassCastException: com.bustracker.MainActivity cannot be cast to android.content.BroadcastReceiver

我有一个 MainActivity class,其中包含 BroadcastReceiver 以捕获系统广播(Internet 连接和 wifi 扫描结果)。我遇到一个问题,正在接收互联网连接广播并将其显示为 Toast,但程序未达到 wifi 扫描结果条件 "if (intent.getAction().equals(android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))"。有谁知道问题出在哪里? manifest中receiver的定义有问题吗?

04-08 23:16:34.319: E/AndroidRuntime(13291): FATAL EXCEPTION: main
04-08 23:16:34.319: E/AndroidRuntime(13291): Process: com.bustracker, PID: 13291
04-08 23:16:34.319: E/AndroidRuntime(13291): java.lang.RuntimeException: Unable to instantiate receiver com.bustracker.MainActivity: java.lang.ClassCastException: com.bustracker.MainActivity cannot be cast to android.content.BroadcastReceiver
04-08 23:16:34.319: E/AndroidRuntime(13291):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2518)
04-08 23:16:34.319: E/AndroidRuntime(13291):    at android.app.ActivityThread.access00(ActivityThread.java:161)
04-08 23:16:34.319: E/AndroidRuntime(13291):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
04-08 23:16:34.319: E/AndroidRuntime(13291):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-08 23:16:34.319: E/AndroidRuntime(13291):    at android.os.Looper.loop(Looper.java:157)
04-08 23:16:34.319: E/AndroidRuntime(13291):    at android.app.ActivityThread.main(ActivityThread.java:5356)
04-08 23:16:34.319: E/AndroidRuntime(13291):    at java.lang.reflect.Method.invokeNative(Native Method)
04-08 23:16:34.319: E/AndroidRuntime(13291):    at java.lang.reflect.Method.invoke(Method.java:515)
04-08 23:16:34.319: E/AndroidRuntime(13291):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
04-08 23:16:34.319: E/AndroidRuntime(13291):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
04-08 23:16:34.319: E/AndroidRuntime(13291):    at dalvik.system.NativeStart.main(Native Method)
04-08 23:16:34.319: E/AndroidRuntime(13291): Caused by: java.lang.ClassCastException: com.bustracker.MainActivity cannot be cast to android.content.BroadcastReceiver
04-08 23:16:34.319: E/AndroidRuntime(13291):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2513)
04-08 23:16:34.319: E/AndroidRuntime(13291):    ... 10 more

Activity代码:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(
                android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {

        } else if (intent.getAction().equals(
                android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {

        }
    }
};

protected void onResume() {
    IntentFilter conn = new IntentFilter();
    conn.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(receiver, conn);

    IntentFilter wifi = new IntentFilter();
    wifi.addAction(android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    registerReceiver(receiver, wifi);

    super.onResume();
}

@Override
protected void onPause() {
    unregisterReceiver(receiver);
    super.onPause();
}

}

您有一个 <receiver> 元素指向 MainActivity

首先,这是错误的,因为 MainActivity 不是 BroadcastReceiver。这就是您的异常告诉您的内容。

其次,您通过registerReceiver()注册了这些广播。 非常 不太可能同时 在清单 中注册 [=14] =].

我将从注释掉 <receiver> 元素开始(因为它显然是错误的)并从那里继续。