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)
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With