A.Alqadomi
A.Alqadomi

Reputation: 1589

Android AsyncTask Strange Compilation Error

I created this simple code but didn't know what causing the compilation error

import java.util.List;
import android.os.AsyncTask;

public class TestAsyncInternalClass {
    private class MyAsyncTask extends AsyncTask<Void, Void, List<InternalPrivateClass>> {
        @Override
        protected List<InternalPrivateClass> doInBackground(Void... params) {
            // TODO Auto-generated method stub
            return null;
        }

        private class InternalPrivateClass{

        }   
    }
}

it is giving me the following compilation error

InternalPrivateClass cannot be resolved to a type

Moving the InternalPrivateClass to the top class level would solve the issue but what is causing this behavior ?

Upvotes: 0

Views: 57

Answers (3)

Jorgesys
Jorgesys

Reputation: 126445

Be sure to have the class InternalPrivateClass and just add the missing import of InternalPrivateClass

Use the shortkey to resolve the imports. Ctrl+shift+O

Upvotes: 0

VMRuiz
VMRuiz

Reputation: 1981

When you declare:

private class MyAsyncTask extends AsyncTask<Void, Void, List<InternalPrivateClass>>

InternalPrivateClass hasn't been declared yet and this is why the compiler show that error.

Upvotes: 1

BVB
BVB

Reputation: 5420

The problem lies in the fact that you are using the private inner class in the template:

extends AsyncTask<Void, Void, List<InternalPrivateClass>>

Moving InternalPrivateClass out of the AsyncTask fixes the issue as it places it in a scope that is visible to the where MyAsyncTask is being defined.

If you change the above to

extends AsyncTask<Void, Void, List<MyAsyncTask.InternalPrivateClass>>

you will get a different error stating that InternalPrivateClass has private access and is therefore inaccessible from the scope you would like to use it in.

Your two options are:

  1. Move the InnerPrivateClass out of MyAsyncTask
  2. Make InnerPrivateClass a public class and use MyAsyncTask.InnerPrivateClass in the template

Upvotes: 1

Related Questions