Nik*_*lin 17 android broadcastreceiver android-intent
我在主要活动和后台服务中创建了一个广播接收器,它正在发送广播意图.每次我尝试运行它时应用程序崩溃,日志显示以下错误消息:
10-04 13:30:43.218:ERROR/AndroidRuntime(695):java.lang.RuntimeException:在com.client中接收广播Intent {action = com.client.gaitlink.CommunicationService.action.LOGIN_STATUS_UPDATE(has extras)}时出错. gaitlink.GaitLink$LoginStatusReceiver@431690e8
通过以下方法从CommunicationService类发送广播消息:
private void announceLoginStatus(){
Intent intent = new Intent(LOGIN_STATUS_UPDATE);
intent.putExtra(SERVER_MESSAGE, mServerResponseMessage);
intent.putExtra(SESSION_STRING, mSessionString);
sendBroadcast(intent);
}
Run Code Online (Sandbox Code Playgroud)
哪里
String LOGIN_STATUS_UPDATE = "com.client.gaitlink.CommunicationService.action.LOGIN_STATUS_UPDATE"
Run Code Online (Sandbox Code Playgroud)
在主要活动中,定义了以下广播接收者:
public class LoginStatusReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String serverMessage = intent.getStringExtra(CommunicationService.SERVER_MESSAGE);
String sessionString = intent.getStringExtra(CommunicationService.SESSION_STRING);
userInfo.setSessionString(sessionString);
saveSettings();
}
}
Run Code Online (Sandbox Code Playgroud)
并在onResume方法中注册:
IntentFilter loginStatusFilter;
loginStatusFilter = new IntentFilter(CommunicationService.LOGIN_STATUS_UPDATE);
loginStatusReceiver = new LoginStatusReceiver();
registerReceiver(loginStatusReceiver, loginStatusFilter);
Run Code Online (Sandbox Code Playgroud)
清单文件包括以下内容:
<activity android:name=".GaitLink"
android:label="@string/app_name">
<intent-filter>
...
<action android:name="com.client.gaitlink.CommunicationService.action.LOGIN_STATUS_UPDATE" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
如果有人能解释为什么Log显示上面的消息并且应用程序崩溃,我将非常感激.
谢谢!
小智 9
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);当你开始新的时候,我通过添加来解决它activity.
如果不是从a开始activity,intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);则需要.
你有两个Intent过滤器; 你只需要一个.如果注册BroadcastReceivervia registerReceiver(),只使用IntentFilter那个API调用的第二个参数 - 不要在清单中放置一个<intent-filter>元素<activity>.
我不确定这是不是你的问题,但肯定没有帮助.
您是否在调试器中一步一步地找到崩溃发生的确切位置?
需要注意的一件事是,如果您要覆盖任何 Android 生命周期事件,您是否在必要时正确调用了基类的构造函数。
小智 5
当您查看错误日志时,您只是查看前几行。但令人惊讶的是,实际问题在下面的几行中提到了-
Fatal Error : Main
error receiving broadcast...bla bla bla <- you are just looking here only
at x.y.z.....
at x.y.z......
at x.y.z.....
at x.y.z......
caused by : ........................... <- but actual problem is here!
at x.y.z.....
at x.y.z......
at x.y.z.....
at x.y.z......
Run Code Online (Sandbox Code Playgroud)