1

My app is trying to get the USB device list. My class extends Service but when I try use AsyncTask a NullPointerException occurs.

@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) 

I think the context could be the problem, but I don't know how to get a context on service.

David Ewen
  • 3,632
  • 1
  • 19
  • 30
hyunwookcho
  • 65
  • 1
  • 6

3 Answers3

1

You may use getApplicationContext() inside your service to get the application context.

And in your case you are try to hold Main Activity Context that is UI part and does not work in Background Services.

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

Use getApplicationContext(); instead of your MainActivity for context.

Do not make getters for your Activities since once they are destroyed they can't be reached. It will always result in a nullpointer. For context you can take it from your Application which lives through the entire life of your app. Hope this helps.

T.Dimitrov
  • 157
  • 2
  • 7
0

If your class extends Service means use service context don't use Activity context and when initialize AsyncTask from serivce pass context

start task by passing context

new DoSomething(this).execute();

in AsyncTask class do like this

class DoSomething extends AsyncTask<Void, String, String> {
        private Context mContext;

        public DoSomething(Context context) {
            mContext = context;
        }

        @Override
        protected String doInBackground(Void... voids) {
            /*Use Context here*/

            return null;
        }
    }
Hemanth S
  • 669
  • 6
  • 16