Java.lang.RuntimeException 接收 BroadCast Intent 时出错

标签 java android android-intent broadcastreceiver

我正在从具有 UI 的 Activity 发送这样的广播

//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");

我的 list 声明:

        <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 调用之前设置日历(实际上是在你的系统与服务绑定(bind)之前)这就是它抛出空指针异常的原因 .在 onServiceConnected 方法

中添加您的 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
        }

关于Java.lang.RuntimeException 接收 BroadCast Intent 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28118999/

相关文章:

java - 如何修复:Azure Cosmos DB SQL API似乎不起作用

java - 使用 Saxon 进行 XPath 评估不适用于节点名

java - 使用 Eclipse 在 Android 虚拟机中启动 Android java 项目时出现问题

java - 解码时未找到类

android org.xmlpull.v1.xmlpullparserexception expected start_tag http//schemas.xmlsoap.org/soap/envelope/envelope 位置开始标记

java - 使用 Java 和 FOP 设置 PDF 标题

java - JDBC和JDBI有什么区别?

android - 方向更改时出现内存不足错误

java - Android编程——制作URI获取音频位置

Android:如何将对象传递给新的启动 Activity