7

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)
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
Sarim Javaid Khan
  • 810
  • 15
  • 30
  • As the stack trace points out, you have a `NullPointerException` on line 92 of `SearchResults.java`, in the `onCreateView()` method of your `SearchResults` class. – CommonsWare Mar 16 '13 at 22:36

3 Answers3

8

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().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • but i also tried to pass just "getActivity()" , even then it crashed. Actually, i want to bind a listview to the arrayadapter. – Sarim Javaid Khan Mar 16 '13 at 22:32
  • @SarimJavaidKhan: "but i also tried to pass just "getActivity()" , even then it crashed" -- then you have a problem somewhere else. Look at the stack trace in LogCat and figure out where you are going wrong. – CommonsWare Mar 16 '13 at 22:35
  • This is the chunk of mycode, i can't figure out if any thing is wrong in it. String[] values = new String[] { "Android", "iPhone", "WindowsMobile","Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" }; ArrayAdapter searchResultsArrayAdapter=new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1,values ); ListView searchResultsListView=(ListView) getActivity().findViewById(R.id.searchResultsListView); searchResultsListView.setAdapter(searchResultsArrayAdapter); – Sarim Javaid Khan Mar 16 '13 at 22:41
  • @SarimJavaidKhan: I repeat, as the stack trace points out, you have a `NullPointerException` on line 92 of `SearchResults.java`, in the `onCreateView()` method of your `SearchResults` class. We have no way of helping you unless you indicate what line 92 is. That being said, you should not be calling `findViewById()` on the `Activity` from within `onCreateView()` of the fragment. The *point* behind `onCreateView()` is for you to *create* the *view*, so you presumably are creating the `ListView` here. Find it from the layout you inflated, as the activity will not have it yet. – CommonsWare Mar 16 '13 at 22:44
0

Got the answer. The problem was in finding the view by id.

getActivity().findViewById();

I used a View variable and inflated the layout, and used that View to find the view.

i.e

View v=inflater.inflate(............) listView listView=v.findViewById(param);

It worked.

Thanx For the help guys.

Sarim Javaid Khan
  • 810
  • 15
  • 30
0

I tried below one and its working perfectly

     // In getView
     FragmentD f = new FragmentD();
     FragmentTransaction ft = context.getFragmentManager().beginTransaction();
     ft.replace(R.id.frame_container, f);
     ft.commit();
*

but in Case of Fragment* ArrayAdapter Class- context type is Activity not Context context like below one:

    public class CListAdapter extends ArrayAdapter<List1> 
    {
        private final Activity context;

        private ArrayList<List1> conlist = null;

        public CListAdapter(Activity context,ArrayList<ConnectorsList> connList) 
        {
            super(context, R.layout.activity_listitem, connList);
            System.out.println("Calling------");
            this.context = context;
            this.conlist = connList;
}
martynas
  • 12,120
  • 3
  • 55
  • 60
ranjan
  • 136
  • 1
  • 12