Hi..

I am trying to make a broadcast receiver that receives BOOT_COMPLETED when the decvice is powered up; and eventually I want to launch my application. Here's my code:

MainActivity.java

BroadcastReceiver mReceiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //receiver
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_BOOT_COMPLETED);
    mReceiver = new Receiver();
    registerReceiver(mReceiver, filter);
}

Receiver.java

public class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // display received action on the screen
        StaticUtil.toastString(intent.getAction());     
    }
}

Manifest

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application>
    <receiver android:name="com.test.project.Receiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

The problem is,

My broadcast receiver well receives SCREEN_ON and SCREEN_OFF nicely. But when the device is restarted, the application gets unfortunately stopped because the Receiver does not exist. The error message on my LogCat is:

java.lang.RuntimeException: unable to start receiver: java.lang.NullPointerException

In fact, this error message is not very surprising because my application has been terminated during device reboot. What should I add or modify in my code? Please help

Solution:

StaticUtil.toastString(intent.getAction()); made error because the application was turned off so there is no such function to call in memory.

Everything works except this