简体   繁体   中英

Cannot infer type arguments for ArrayAdapter<>

I am still playing around with my calendar, I already nearly managed to integrate the https://github.com/SundeepK/CompactCalendarView into one of my fragments. There is just one error left, i did some research, others got the problem too for example with ArrayList<>.

Example code:

final ArrayAdapter adapter = new ArrayAdapter<>
            (this,android.R.layout.simple_list_item_1, mutableBookings);

The IDE says:

Error:(87, 38) error: cannot infer type arguments for ArrayAdapter<>

Note: C:...\Uebersicht.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.

I already tried to recompile, but the result doesn't seem to work

Uebersicht.java:87: error: cannot find symbol
    final ArrayAdapter adapter = 
          new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, mutableBookings);


Uebersicht.java:87: error: cannot find symbol 
final ArrayAdapter adapter = 
          new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mutableBookings);    


Uebersicht.java:87: error: package android.R does not exist
    final ArrayAdapter adapter = 
          new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mutableBookings);    

I can post my full Fragment class too if it's necessary, it must be something with my API and the API the ArrayAdapter is using? Don't forget I am just a beginner, I am trying to do just some stuff all by myself.

You need to pass a Context to the Constructor of ArrayAdapter. You're actually in initializing it in a Fragment class, so this is not valid as a Context. Try calling

final ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), 
                                android.R.layout.simple_list_item_1,
                                mutableBookings);

Sometimes the methods do not run directly in the activity and do not have access to it, not because of this, but because of the getApplicationContext ()

    ArrayAdapter adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, mutableBookings);

Try using type in your Adapter declaring. You are missing the type of your mutableBookings :

final ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mutableBookings);

If you are in a Fragment so you should use

ArrayAdapter<String> adapter = new ArrayAdapter<>(**getContext()**,android.R.layout.simple_list_item_1,items);

I got the same error as error: cannot infer type arguments for ArrayAdapter<> when I was using the code below:

ArrayList<String> arrayListward_items=new ArrayList<>(Arrays.asList(wardItemsKisumuSeme));
ArrayAdapter<String> ward_itemsAdapter=new ArrayAdapter<>(this,R.layout.style_spinner,arrayListward_items);
                    wardSpinner.setAdapter(ward_itemsAdapter);

I changed the this to getApplicationContext() and was able to fix the error, try that:

ArrayList<String> arrayListward_items=new ArrayList<>(Arrays.asList(wardItemsKisumuSeme));
ArrayAdapter<String> ward_itemsAdapter=new ArrayAdapter<>(getApplicationContext(),R.layout.style_spinner,arrayListward_items);
                    wardSpinner.setAdapter(ward_itemsAdapter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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