引起:java.lang.ClassCastException:com.bustracker.MainActivity无法强制转换为android.content.BroadcastReceiver

时间:2015-04-08 21:33:03

标签: android broadcastreceiver

我有一个MainActivity类,它包含BroadcastReceiver以捕获系统广播(Internet连接和wifi扫描结果)。我正面临一个问题,互联网连接广播正在接收并显示为Toast但是wifi扫描结果条件" if(intent.getAction()。equals(android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))&#34 ;没有从该计划达成。有谁知道问题出在哪里?清单中接收器的定义有问题吗?

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.access$1800(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

活动代码:

公共类MainActivity扩展了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();
}

}

1 个答案:

答案 0 :(得分:2)

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

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

其次,您通过registerReceiver()注册这些广播。您希望非常 不太可能在清单中注册并通过registerReceiver()注册。

我首先要评论<receiver>元素(因为它显然是错误的)并从那里开始。