Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayAdapter Android without Context

I want to implement an ArrayAdapter in my class which extends Fragment not Activity.

The problem is what to pass in the first parameter of the constructor of ArrayAdapter.

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(<what place here>,android.R.layout.simple_list_item1,anyarraylist);

Now what should i do with the first parameter? I tried to pass

getActivity().getApplicationContext()

in the first parameter but the code crashed.

Please help me out.

My logcat:

03-17 03:33:08.029: E/AndroidRuntime(596): FATAL EXCEPTION: main
03-17 03:33:08.029: E/AndroidRuntime(596): java.lang.NullPointerException
03-17 03:33:08.029: E/AndroidRuntime(596):  at com.search.pages.SearchResults.onCreateView(SearchResults.java:92)
03-17 03:33:08.029: E/AndroidRuntime(596):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
03-17 03:33:08.029: E/AndroidRuntime(596):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
03-17 03:33:08.029: E/AndroidRuntime(596):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
03-17 03:33:08.029: E/AndroidRuntime(596):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
03-17 03:33:08.029: E/AndroidRuntime(596):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
03-17 03:33:08.029: E/AndroidRuntime(596):  at android.os.Handler.handleCallback(Handler.java:587)
03-17 03:33:08.029: E/AndroidRuntime(596):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 03:33:08.029: E/AndroidRuntime(596):  at android.os.Looper.loop(Looper.java:123)
03-17 03:33:08.029: E/AndroidRuntime(596):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-17 03:33:08.029: E/AndroidRuntime(596):  at java.lang.reflect.Method.invokeNative(Native Method)
03-17 03:33:08.029: E/AndroidRuntime(596):  at java.lang.reflect.Method.invoke(Method.java:521)
03-17 03:33:08.029: E/AndroidRuntime(596):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-17 03:33:08.029: E/AndroidRuntime(596):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-17 03:33:08.029: E/AndroidRuntime(596):  at dalvik.system.NativeStart.main(Native Method)
like image 480
Sarim Javaid Khan Avatar asked Mar 16 '13 22:03

Sarim Javaid Khan


1 Answers

now what should i do with the first parameter

getActivity() returns an Activity, which will work well as your first parameter to your ArrayAdapter constructor. Activity inherits from Context.

I tried to pass "getActivity().getApplicationContext()" in the first parameter but the code crashed

Never call getApplicationContext() unless you know precisely why you are calling getApplicationContext().

like image 168
CommonsWare Avatar answered Oct 20 '22 14:10

CommonsWare