Cannot resolve method 'getActivity()' which I am using to initialize Adapter

cannot resolve method android studio
cannot resolve method intellij
cannot resolve method findviewbyid
cannot resolve method findviewbyid in fragment
cannot resolve method void
cannot resolve method getdefaultinstance
cannot resolve method value
cannot resolve method get int

I am creating a custom Adapter MyAdapter and inflating a ListView listView to display an array of String data. I am getting error while initializing MyAdapter using getActivity() method, as follows:

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private String[] data = {"Sanskrit", "Bhojpuri", "Newari", "Magar",
            "Gurung", "English", "Maithili", "Tamil", "Hindi",
            "French", "German", "Russian", "Chinese"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.list_view);

        MyAdapter adapter = new MyAdapter(getActivity(), data);
        listView.setAdapter(adapter);
    }
}

class MyAdapter extends ArrayAdapter {
    String[] data;

    public MyAdapter(Context context, int res, String[] data) {
        super(context,res);

        this.data = data;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) getContext().
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View row = inflater.inflate(R.layout.custom_list_view, parent, false);

        TextView textView = row.findViewById(R.id.textView);

        textView.setText(data[position]);
        return row;
    }
}

Any help would be appreciated.


You're already in the Activity when you initialize MyAdapter. The Activity class has no getActivity() method because it's already the Activity.

Use this:

MyAdapter adapter = new MyAdapter(this, data);

Getting "cannot resolve method" error when trying to implement , getSharedPreferences() needs a context to be accessed. For instance: mContext.​getSharedPreferences(PREF_NAME, Context. Without new methods application seems working. In other project everything is ok. now i realize that something is wrong with maven libraries . Things i tried so far and didnt work:


I guess you copied the code from the net. You should use getActivity() when you work with a fragment to refer to the host activity's context, but since you're in an activity you only need this to get the current activity's context.

MyAdapter adapter = new MyAdapter(this, data);

[Solved] How do I resolve the the cannot resolve method , You need to first call the getView() function to get the view of the fragment and then you can call the findViewById() . Like this,. Hide Copy Code. Make sure your container (using generics) holds the Question type: ArrayList<Question> questions = new ArrayList<Question>(); That way JAVA knows which method to call.


getActivity() method returns the context of the activity.

Try this

MyAdapter adapter = new MyAdapter(YourActivityName.this,data);

If you are wanting the current activity then just

MyAdapter adapter = new MyAdapter(this,data);

Cannot resolve method 'get(java.lang.String)' · Issue #238 , Cannot resolve method 'get(java.lang.String)' here.. " @override public int getItemViewType(int position) { Contribution contribution = contributions.get(​position);  It's not only that method that cannot be resolved. "AppCompatActivity" is also red as well as "onCreate" and "setContentView". Posting to the forum is only allowed for members with active accounts.


you can get the context in many ways:

    MyAdapter adapter = new MyAdapter(getBaseContext(),data);
    listView.setAdapter(adapter);

or you can use the application context:

    MyAdapter adapter = new MyAdapter(getApplicationContext(),data);
    listView.setAdapter(adapter);

Or the current activity context:

    MyAdapter adapter = new MyAdapter(this,data);
    listView.setAdapter(adapter);

cannot resolve method .getService(); · Issue #2548 · googleapis , cannot resolve method .getService(); #2548. Closed. RakeshSiddu opened this issue on Oct 23, 2017 · 7 comments. Closed  and Android Studio is telling "Cannot resolve method 'makeText(com.gabas.funfacts.FunFactsActivity, java.lang.String, int)' the code is with a red line below. Daniel Bowen


Here's the solution.

 MyAdapter adapter = new MyAdapter(getActivity(),data); 
    ...

to

MyAdapter adapter = new MyAdapter(this,R.layout.custom_list_view,data);

and

public MyAdapter(Context context,int res,String[] data) {
super(context,res);

to

public MyAdapter(Context context,int resource,String[] data) {
super(context,resource,data);

I was passing wrong arguments while initializing the MyAdapter instance and while creating MyAdapter constructor and calling super.

Thanks everyone for your answer. ;)

Intellij cannot resolve method but compiles and runs – IDEs Support , However, Intellij cannot resolve methods in most of my classes. It compiles, runs and Maven does not complain. I have spent a lot of time  The migration succeded and the project runs fine. However, Intellij cannot resolve methods in most of my classes. It compiles, runs and Maven does not complain. I have spent a lot of time installing and re-installing Intellij, invalidate cache and restart, deleted .idea and iml files, etc. Nothing works and still cannot resolve methods.


Cannot resolve method "getName()', Hey guys, I am making GUI plugin for our server. I have a problem with my code. Could somebody check my code and tell me what is wrong  Recipient.Resolve method (Outlook) 06/08/2017; 2 minutes to read +2; In this article. Attempts to resolve a Recipient object against the Address Book. Syntax. expression. Resolve. expression A variable that represents a Recipient object. Return value. True if the object was resolved; otherwise, False. Example


Java Errors, java:5: cannot resolve symbol symbol : method Println (java.lang.String) location: class java.io.PrintStream System.out.Println("Hello world");. OK, it's another  This method encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array. Syntax. Here is the syntax of this method − public byte[] getBytes() Return Value. This method returns the resultant byte array. Example


Android Studio is telling me it cannot resolve method (?), and Android Studio is telling "Cannot resolve method 'makeText(com.gabas.​funfacts.FunFactsActivity, java.lang.String, int)'. the code is with a  > delete JDK configuration? do you mean to uninstall JDK completely? No, just delete JSDK in IDEA and add it again. > I can not quite follow "try File | Invalidate Caches | Invalidate and Restart."