简体   繁体   中英

How to fix BootReciever java.lang.NullPointerException?

I am getting this error and I don't know how to fix it:

09-18 10:14:34.482: E/Trace(787): error opening trace file: No such file or directory (2)
09-18 10:14:34.542: W/ResourceType(787): No package identifier when getting value for resource number 0x00000000
09-18 10:14:34.683: E/BootReceiver(787): java.lang.NullPointerException

can someone tell me how to fix this error,

BroadcastReceiver java:

public class BootReceiver extends BroadcastReceiver {

    private static final String TAG="BootReceiver";

    @Override 
    public void onReceive(Context context,Intent intent) {
        try {
            DisplayMetrics metrics = new DisplayMetrics(); 
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            windowManager.getDefaultDisplay().getMetrics(metrics);
            int height = metrics.heightPixels; 
            int width = metrics.widthPixels;
            Bitmap tempbitMap = BitmapFactory.decodeResource(context.getResources(), MainActivity.tophone);
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); 
            wallpaperManager.setWallpaperOffsetSteps(1, 1);
            wallpaperManager.suggestDesiredDimensions(width, height);
            try {
                wallpaperManager.setBitmap(tempbitMap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch(Exception e){
            Log.e(TAG,e.toString());
        }
    }
}

Application is unable to find MainActivity.tophone as your device is booted just and your application is yet no open it mean tophone variable is yet initialized. So it is firing NullPointerException because of No package identifier it mean it is unable to find resource with a given name which actually is null.

Resolve :

  • Declare value of tophone it self in Broadcast
  • Store Value in SharedPreference in activity and fetch it in BroadcastReceiver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM