Java.lang.RuntimeException 接收广播意图时出错
Java.lang.RuntimeException Error Receiving BroadCast Intent
我正在从我的 Activity 发送这样的广播,它有一个 UI
//Code in my UI Activity
final String BROADCAST = "in.itechvalley.notification.MainActivity.android.ACTION.BROADCAST" ;
IntentFilter intentFilter = new IntentFilter("in.itechvalley.notification.MainActivity.android.ACTION.BROADCAST");
in.itechvalley.notification.MainActivity rec = new in.itechvalley.notification.MainActivity();
Context context = getApplicationContext();
context.registerReceiver(rec, intentFilter);
Intent intent = new Intent(BROADCAST);
context.sendBroadcast(intent);
Log.d("MY TAG","Inside Broadcast");
我的清单声明:
<receiver android:name="in.itechvalley.notification.MainActivity"
android:exported="false" >
<intent-filter >
<action android:name="in.itechvalley.notification.MainActivity.android.ACTION.BROADCAST"/>
</intent-filter>
</receiver>
并且我在onReceive()中写了一段代码
@Override //This is inside MainActivity Class that extends BroadcastReceiver
public void onReceive(Context context, Intent intent)
{
Log.d("MY TAG", "Inside the onReceive");
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
c.set(year, month, day);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 3);
c.set(Calendar.SECOND, 0);
Toast.makeText(context, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();
}
但是当我在我的设备上执行这个应用程序时,它停止工作了。我收到错误消息“java.lang.RuntimeException.......我已经粘贴了 LogCat
LOGCAT
01-24 02:43:01.320: E/AndroidRuntime(28802): FATAL EXCEPTION: main
01-24 02:43:01.320: E/AndroidRuntime(28802): Process: in.itechvalley, PID: 28802
01-24 02:43:01.320: E/AndroidRuntime(28802): java.lang.RuntimeException: Error receiving broadcast Intent { act=in.itechvalley.notification.MainActivity.android.ACTION.BROADCAST flg=0x10 } in in.itechvalley.notification.MainActivity@428d1de0
01-24 02:43:01.320: E/AndroidRuntime(28802): at android.os.Handler.handleCallback(Handler.java:733)
01-24 02:43:01.320: E/AndroidRuntime(28802): at android.os.Handler.dispatchMessage(Handler.java:95)
01-24 02:43:01.320: E/AndroidRuntime(28802): at android.os.Looper.loop(Looper.java:136)
01-24 02:43:01.320: E/AndroidRuntime(28802): at android.app.ActivityThread.main(ActivityThread.java:5139)
01-24 02:43:01.320: E/AndroidRuntime(28802): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 02:43:01.320: E/AndroidRuntime(28802): at java.lang.reflect.Method.invoke(Method.java:515)
01-24 02:43:01.320: E/AndroidRuntime(28802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
01-24 02:43:01.320: E/AndroidRuntime(28802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
01-24 02:43:01.320: E/AndroidRuntime(28802): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
01-24 02:43:01.320: E/AndroidRuntime(28802): at dalvik.system.NativeStart.main(Native Method)
01-24 02:43:01.320: E/AndroidRuntime(28802): Caused by: java.lang.NullPointerException
01-24 02:43:01.320: E/AndroidRuntime(28802): at in.itechvalley.notification.MainActivity.onReceive(MainActivity.java:45)
01-24 02:43:01.320: E/AndroidRuntime(28802): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:766)
注意-我已经阅读了几乎所有与此相关的 SO 线程,但找不到解决方案。我非常确定这是一个 NPE,但我不知道我从哪里得到 NPE?
PS - MainActivity 没有任何用户界面,而 UIActivity 有用户界面并且 UIActivity 被调用首先在应用程序启动时
提前感谢您的帮助
已更新
ScheduleClient.java
`public ScheduleClient(Context context) {
mContext = context;
}
/**
* Call this to connect your activity to your service
*/
public void doBindService() {
// Establish a connection with our service
mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
/**
* When you attempt to connect to the service, this connection will be called with the result.
* If we have successfully connected we instantiate our service object so that we can call methods on it.
*/
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with our service has been established,
// giving us the service object we can use to interact with our service.
mBoundService = ((ScheduleService.ServiceBinder) service).getService();
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
/**
* Tell our service to set an alarm for the given date
* @param c a date to set the notification for
*/
public void setAlarmForNotification(Calendar c)
{
mBoundService.setAlarm(c);
}
/**
* When you have finished with the service call this method to stop it
* releasing your connection and resources
*/
public void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
mContext.unbindService(mConnection);
mIsBound = false;
}
}
Logcat
说你试图在 onServiceConnected
调用之前设置日历(实际上是在你的系统与服务绑定之前)这就是它抛出空指针异常的原因
.在 onServiceConnected
method
中添加您的 setAlarmForNotification(Calendar c) 调用
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with our service has been established,
// giving us the service object we can use to interact with our service.
mBoundService = ((ScheduleService.ServiceBinder) service).getService();
//call your setcalendar here
}
我正在从我的 Activity 发送这样的广播,它有一个 UI
//Code in my UI Activity
final String BROADCAST = "in.itechvalley.notification.MainActivity.android.ACTION.BROADCAST" ;
IntentFilter intentFilter = new IntentFilter("in.itechvalley.notification.MainActivity.android.ACTION.BROADCAST");
in.itechvalley.notification.MainActivity rec = new in.itechvalley.notification.MainActivity();
Context context = getApplicationContext();
context.registerReceiver(rec, intentFilter);
Intent intent = new Intent(BROADCAST);
context.sendBroadcast(intent);
Log.d("MY TAG","Inside Broadcast");
我的清单声明:
<receiver android:name="in.itechvalley.notification.MainActivity"
android:exported="false" >
<intent-filter >
<action android:name="in.itechvalley.notification.MainActivity.android.ACTION.BROADCAST"/>
</intent-filter>
</receiver>
并且我在onReceive()中写了一段代码
@Override //This is inside MainActivity Class that extends BroadcastReceiver
public void onReceive(Context context, Intent intent)
{
Log.d("MY TAG", "Inside the onReceive");
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
c.set(year, month, day);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 3);
c.set(Calendar.SECOND, 0);
Toast.makeText(context, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();
}
但是当我在我的设备上执行这个应用程序时,它停止工作了。我收到错误消息“java.lang.RuntimeException.......我已经粘贴了 LogCat
LOGCAT
01-24 02:43:01.320: E/AndroidRuntime(28802): FATAL EXCEPTION: main
01-24 02:43:01.320: E/AndroidRuntime(28802): Process: in.itechvalley, PID: 28802
01-24 02:43:01.320: E/AndroidRuntime(28802): java.lang.RuntimeException: Error receiving broadcast Intent { act=in.itechvalley.notification.MainActivity.android.ACTION.BROADCAST flg=0x10 } in in.itechvalley.notification.MainActivity@428d1de0
01-24 02:43:01.320: E/AndroidRuntime(28802): at android.os.Handler.handleCallback(Handler.java:733)
01-24 02:43:01.320: E/AndroidRuntime(28802): at android.os.Handler.dispatchMessage(Handler.java:95)
01-24 02:43:01.320: E/AndroidRuntime(28802): at android.os.Looper.loop(Looper.java:136)
01-24 02:43:01.320: E/AndroidRuntime(28802): at android.app.ActivityThread.main(ActivityThread.java:5139)
01-24 02:43:01.320: E/AndroidRuntime(28802): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 02:43:01.320: E/AndroidRuntime(28802): at java.lang.reflect.Method.invoke(Method.java:515)
01-24 02:43:01.320: E/AndroidRuntime(28802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
01-24 02:43:01.320: E/AndroidRuntime(28802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
01-24 02:43:01.320: E/AndroidRuntime(28802): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
01-24 02:43:01.320: E/AndroidRuntime(28802): at dalvik.system.NativeStart.main(Native Method)
01-24 02:43:01.320: E/AndroidRuntime(28802): Caused by: java.lang.NullPointerException
01-24 02:43:01.320: E/AndroidRuntime(28802): at in.itechvalley.notification.MainActivity.onReceive(MainActivity.java:45)
01-24 02:43:01.320: E/AndroidRuntime(28802): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:766)
注意-我已经阅读了几乎所有与此相关的 SO 线程,但找不到解决方案。我非常确定这是一个 NPE,但我不知道我从哪里得到 NPE?
PS - MainActivity 没有任何用户界面,而 UIActivity 有用户界面并且 UIActivity 被调用首先在应用程序启动时
提前感谢您的帮助
已更新
ScheduleClient.java
`public ScheduleClient(Context context) {
mContext = context;
}
/**
* Call this to connect your activity to your service
*/
public void doBindService() {
// Establish a connection with our service
mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
/**
* When you attempt to connect to the service, this connection will be called with the result.
* If we have successfully connected we instantiate our service object so that we can call methods on it.
*/
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with our service has been established,
// giving us the service object we can use to interact with our service.
mBoundService = ((ScheduleService.ServiceBinder) service).getService();
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
/**
* Tell our service to set an alarm for the given date
* @param c a date to set the notification for
*/
public void setAlarmForNotification(Calendar c)
{
mBoundService.setAlarm(c);
}
/**
* When you have finished with the service call this method to stop it
* releasing your connection and resources
*/
public void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
mContext.unbindService(mConnection);
mIsBound = false;
}
}
Logcat
说你试图在 onServiceConnected
调用之前设置日历(实际上是在你的系统与服务绑定之前)这就是它抛出空指针异常的原因
.在 onServiceConnected
method
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with our service has been established,
// giving us the service object we can use to interact with our service.
mBoundService = ((ScheduleService.ServiceBinder) service).getService();
//call your setcalendar here
}