I have written the following code snippet in my App:
public Fragment getItem(int position) {
switch(position){
case 0:
LP_Events_Tab mEventsTab= new LP_Events_Tab();
mList= (ListView) findViewById(R.id.event_list);
String[] temp= {"1","2","3"};
ArrayAdapter<String> arrayAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, temp);
return mEventsTab;
/*Other cases*/
}
return null;
}
For case 0, I am using an array of strings named temp
to initialize the ArrayAdapter
. However, Android Studio redlines the ArrayAdapter
initialization arguments: (this,android.R.layout.simple_list_item_1, temp)
and thus, I cannot proceed. I do not see any error with these arguments but Android Studio does and hence, I am stuck.
Replace the following line of code inside your Fragment -
ArrayAdapter<String> arrayAdapter= new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, temp);
Hope this helps, let me know if you need any more help.
The problem is with your Constructor of Adapter
ArrayAdapter(Context context, int resource, List<T> objects)
So make sure you are passing right parameters, in your case your first parameter is this
which will not work other then in Activity. So pass either getContext
or getActivity
.
尝试用“ MainActivity.this”(或您拥有的任何类名)替换“ this”
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.