简体   繁体   English

如何修复BootReciever java.lang.NullPointerException?

[英]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: 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. 刚启动设备且应用程序尚未打开时,应用程序找不到MainActivity.tophone ,这意味着tophone变量尚未初始化。 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. 由于No package identifier ,因此触发了NullPointerException ,这意味着它无法找到具有给定名称的资源,该名称实际上为空。

Resolve : 解决:

  • Declare value of tophone it self in Broadcast 在广播中自行声明电话的价值
  • Store Value in SharedPreference in activity and fetch it in BroadcastReceiver 将值存储在活动中的SharedPreference中,并在BroadcastReceiver中获取它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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