0

I am beginner in android. I was having a requirement similar to what is discussed here: Separate Back Stack for each tab in Android using Fragments.

Now, I am using that project given here gitHub

What I want is to add listview to a fragment..Further on clicking a particular item in list another fragment is visible.

But I am facing a problem in doing it. For eg here,I get error in using setListAdapter(adapter) function. Plz help.

public class AppTabAFirstFragment extends BaseFragment {
private Button mGotoButton;
//private String[] characters= {"shs","sds","sdss","sdsd"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View view       =   inflater.inflate(R.layout.app_tab_a_first_screen, container, false);
    mGotoButton =   (Button) view.findViewById(R.id.id_next_tab_a_button);
    mGotoButton.setOnClickListener(listener);
   // ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(),R.layout.app_tab_d_first_screen, characters);
   // setListAdapter(adapter);

    return view;

}

In this way,it doesn't allow us to use setListAdapter() function reason being I can't extend ListActivity. I am all confused. Kindly help.

Community
  • 1
  • 1
Harish Vats
  • 662
  • 6
  • 21

1 Answers1

0

I haven't checked your links, but as I can see from the little code you posted I think this might be the problem:

You need to lookup your listview from the xml file (if you have defined it there) and set the adapter to that object. For instance:

ListView listView = (ListView) view.findViewById(R.id.my_list_view);
listView.setAdapter(adapter);
Zezeq
  • 119
  • 7
  • No... Its not helping. After using it I am getting an Null pointer exception W/dalvikvm(1983): threadid=1: thread exiting with uncaught exception (group=0xa6276288) E/AndroidRuntime(1983): java.lang.NullPointerException at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392) at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) at android.widget.AbsListView.obtainView(AbsListView.java:2267) at android.widget.ListView.measureHeightOfChildren(ListView.java:1244) at android.widget.ListView.onMeasure(ListView.java:1156) at android.view.View.measure(View.java:15172) – Harish Vats Jul 25 '14 at 11:01
  • have you change id of list view in xml? @android:id/list to @+id/my_list_view – Jayesh Khasatiya Jul 25 '14 at 11:21
  • also Replace inflater.getContext() to getActivity() in Adapter Declaration. – Jayesh Khasatiya Jul 25 '14 at 11:22
  • 1
    Looking at the error log you posted now (it could have been helpful from the start) is that there is an error with your adapter. I do not know how you implemented your layout (R.layout.app_tab_d_first_screen), but try using the android standard android.R.layout.simple_list_item_1 instead and se if it works. Have you uncommented the object characters? If it doesn't work post your full error log – Zezeq Jul 25 '14 at 11:50
  • @Zezeq wohhooo... just now Early morning I tried this (R.layout.simple_list_item_1)and it worked. Made my day :D :P Thanks a lot. – Harish Vats Jul 28 '14 at 03:58