我的应用正在尝试获取USB设备列表。我的班级extends
Service
但是当我尝试使用AsyncTask
时,会出现NullPointerException
。
@Override
protected String doInBackground(Void... params) {
Context context = MainActivity.getMainActivity();
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while (deviceIterator.hasNext()) {
UsbDevice device = deviceIterator.next();
String ACTION_USB_PERMISSION = "my packagename.USB_PERMISSION";
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0); //NULLPOINTEXCEPTION;
manager.requestPermission(device, mPermissionIntent);
.
.
}
}
logcat的
Process: blackeyeonandroid.iosystem.co.kr.toastama, PID: 15069
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:526)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:515)
at blackeyeonandroid.iosystem.co.kr.toastama.MyService$DeviceDetectAsync.doInBackground(MyService.java:124)
at blackeyeonandroid.iosystem.co.kr.toastama.MyService$DeviceDetectAsync.doInBackground(MyService.java:105)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
我认为上下文可能是问题,但我不知道如何获得服务上下文。
答案 0 :(得分:1)
您可以在服务中使用getApplicationContext()
来获取应用程序上下文。
在您的情况下,您将尝试保存作为UI部分的主要活动上下文,但在后台服务中不起作用。
答案 1 :(得分:0)
使用getApplicationContext();
代替您的MainActivity用于上下文。
不要为你的活动制作吸气剂,因为一旦它们被销毁,就无法到达。它总是会产生一个nullpointer。对于上下文,您可以从应用程序中获取它,该应用程序贯穿应用程序的整个生命周期。 希望这会有所帮助。
答案 2 :(得分:0)
如果您的课程延长$selectedType = Type::where('slug', '=', $slug)->where('type', '=', 'studio')->with(['apartments' => function ($query) use ($request) {
$query->where('status', '!=', 'sold');
if ($request->floor == 'low') {
$query->where('floor', '<', '5');
} elseif ($request->floor == 'mid') {
$query->where('floor', '>', '1')->where('floor', '<', '6');
} elseif ($request->floor == 'high') {
$query->where('floor', '>', '4');
}
if ($request->sun == 'morning_sun') {
$query->where('sun', '=', 'morning');
} elseif ($request->sun == 'evening_sun') {
$query->where('sun', '=', 'evening');
}
if ($request->price == '1') {
$query->where('price', '<', '900')->orWhere([['price_min', '!=', null], ['price_max', '!=', null], ['price_min', '<', '900']]);
} elseif ($request->price == '2') {
$query->where('price', '<', '999')->orWhere([['price_min', '!=', null], ['price_max', '!=', null], ['price_min', '<', '999']]);
}
}])->first();
,则表示使用服务上下文时请不要使用Service
上下文,并在从serivce传递上下文初始化Activity
时
通过传递上下文来启动任务
AsyncTask
new DoSomething(this).execute();
课程中的就是这样的
AsyncTask