Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
接收广播意图时发生Java.lang.RuntimeException错误_Java_Android_Android Intent_Broadcastreceiver - Fatal编程技术网

接收广播意图时发生Java.lang.RuntimeException错误

接收广播意图时发生Java.lang.RuntimeException错误,java,android,android-intent,broadcastreceiver,Java,Android,Android Intent,Broadcastreceiver,我正在从具有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.

我正在从具有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> 
但当我在我的设备上执行此应用程序时,它会停止工作。我收到一个错误“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
表示您试图在服务连接的调用之前设置日历(实际上是在系统绑定服务之前),这就是它抛出空指针异常的原因 。将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
        }

指向
MainActivity.java
,第45行。将代码张贴在MainActivity的第45行。java@user3249477
scheduleClient.setAlarmForNotification(c)
在MainActivity中发布整个代码。或者至少在第45行附近,在您初始化
scheduleClient
@user3249477的地方,我已经更新了我的答案并添加了scheduleClient类。问题仍然是同一个问题。问题是在我发送广播(来自UIActivity)时产生的。我初始化了
Context Context=null;
并且没有给我任何错误。但是在null Context中
onReceive()
没有执行。我已经在问题中发布了我的全部代码。@Exbury
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
        }