简体   繁体   English

连接状态更改时,BroadcastReceiver使Android应用程序崩溃

[英]BroadcastReceiver crashes Android application when connection state changes

I have the following code: 我有以下代码:

    private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
    /*
    boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
    String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
    boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

    NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
    */
    // do application-specific task(s) based on the current network state, such 
    // as enabling queuing of HTTP requests when currentNetworkInfo is connected etc.
//  Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show();
    //      if(!check3G()||!checkWifi())
        updateUI();
    }
    };
       /*
        * method to be invoked to register the receiver
        */
       private void registerReceivers() {    
           registerReceiver(mConnReceiver, 
               new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
       }

And I register the receiver in the oncreate method like so: 然后像这样在oncreate方法中注册接收器:

   public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);
    registerReceivers();
    /* Code omitted*/
    }

And in ondestroy as 而在毁灭中

    public void onDestroy(){
     unregisterReceiver(mConnReceiver);
    super.onDestroy();      
}

Now whenever the connection state changes, I want to call updateUI(); 现在,只要连接状态发生变化,我都想调用updateUI();。

But when it does change, the function is called, but the application crashes. 但是,当确实发生更改时,将调用该函数,但应用程序将崩溃。 I am unsure what I am doing wrong and any suggestions would be helpful. 我不确定自己在做什么错,任何建议都会有所帮助。

Thanks 谢谢

Edit: 编辑:

LogCat Log: LogCat日志:

09-05 04:04:56.968: I/ActivityManager(246): Start proc edu.ucla.pam for activity edu.ucla.pam/.LoginScreenActivity: pid=7158 uid=10008 gids={3003, 3002, 3001, 1015, 1028}
09-05 04:04:57.757: I/ActivityManager(246): Displayed edu.ucla.pam/.LoginScreenActivity: +856ms
09-05 04:05:23.347: I/ActivityManager(246): START {cmp=edu.ucla.pam/.MenuMainActivity (has extras) u=0} from pid 7158
09-05 04:05:23.574: I/ActivityManager(246): Displayed edu.ucla.pam/.MenuMainActivity: +173ms
09-05 04:05:37.386: E/AndroidRuntime(7158): java.lang.RuntimeException: Unable to instantiate receiver edu.ucla.pam.receiver.ConnectivityReceiver: java.lang.ClassNotFoundException: edu.ucla.pam.receiver.ConnectivityReceiver
09-05 04:05:37.386: E/AndroidRuntime(7158): Caused by: java.lang.ClassNotFoundException: edu.ucla.pam.receiver.ConnectivityReceiver
09-05 04:05:37.398: W/ActivityManager(246):   Force finishing activity edu.ucla.pam/.MenuMainActivity
09-05 04:05:37.925: W/ActivityManager(246): Activity pause timeout for ActivityRecord{4225a750 edu.ucla.pam/.MenuMainActivity}
09-05 04:05:39.296: I/ActivityManager(246): Process edu.ucla.pam (pid 7158) has died.
09-05 04:05:39.296: I/WindowState(246): WIN DEATH: Window{4221fec0 edu.ucla.pam/edu.ucla.pam.LoginScreenActivity paused=false}
09-05 04:05:39.300: I/WindowState(246): WIN DEATH: Window{4250fe58 edu.ucla.pam/edu.ucla.pam.MenuMainActivity paused=false}
09-05 04:05:39.304: W/ActivityManager(246): Force removing ActivityRecord{42153568 edu.ucla.pam/.LoginScreenActivity}: app died, no saved state
09-05 04:05:56.227: I/ActivityManager(246): Start proc edu.ucla.pam for broadcast edu.ucla.pam/.receiver.ConnectivityReceiver: pid=7325 uid=10008 gids={3003, 3002, 3001, 1015, 1028}
09-05 04:05:56.398: E/AndroidRuntime(7325): java.lang.RuntimeException: Unable to instantiate receiver edu.ucla.pam.receiver.ConnectivityReceiver: java.lang.ClassNotFoundException: edu.ucla.pam.receiver.ConnectivityReceiver
09-05 04:05:56.398: E/AndroidRuntime(7325): Caused by: java.lang.ClassNotFoundException: edu.ucla.pam.receiver.ConnectivityReceiver
09-05 04:05:56.398: W/ActivityManager(246): Process edu.ucla.pam has crashed too many times: killing!
09-05 04:05:56.398: I/ActivityManager(246): Killing proc 7325:edu.ucla.pam/u0a8: crash

It turns out the issue was some legacy code. 事实证明,问题是一些旧代码。 There was an additional ConnectivityReceiver registered in the Manifest that was never instantiated that was caused the application to crash. 在清单中注册了一个附加的ConnectivityReceiver,它从未实例化,导致应用程序崩溃。

I will post the legacy code shortly. 我将很快发布旧代码。

Thanks for all your help. 感谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM