ListView becomes null in fragment - android

I am trying to define a listview with a corresponding Adapter to add string to the listview. Howevewr, my listview comes as being null. The code is as below:-
public class CategoryFragment extends Fragment {
....
....
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView listView = (ListView) getActivity().findViewById(R.id.cat_vacancies);
listAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_2);
listView.setAdapter(listAdapter);
....
}
}
What have I done wrong at this point?
my fragment xml is as follows( I have only added the relevant portions which I think may help in diagnosing the problem)
<RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/cat_vacancies">
</ListView>
</RelativeLayout>
P.S. This is, as far as I know, a silly mistake, but I have not yet completely grasped android completely, so excuse my problems.

#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.[layout id], container, false);
ListView listView = (ListView) view.findViewById(R.id.cat_vacancies);
retrun view;
}

Try whit that way
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.yourFragmentXml, container, false);
ListView listView = (ListView) view.findViewById(R.id.cat_vacancies);
listAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_2);
listView.setAdapter(listAdapter);
....
}

Related

Autocomplete Text is not working in android fragment

I want to set autocomplete text in my application. The suggestion values will fetch from the database. But for a trial I made a string array for the suggestion values. It works fine when I implement it on an activity but not working in fragment.
In place of getActivity function I tried:
1) getContext()
2) this.getActivity()
3) (Search_Bus)getActivity()
but none work...
it's giving me error:
Attempt to invoke virtual method on a null object reference
Here's my code...
public class Bus_Schedule_tab3 extends Fragment {
AutoCompleteTextView autoCompleteTextView;
ArrayList<String> array;
ArrayAdapter<String> adapter;
String[] stop_names;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bus_schedule_tab3, container, false);
stop_names = new String[] {"usa","china","russia","bangladesh"};
autoCompleteTextView = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView2);
adapter = new ArrayAdapter<String>(getActivity(),R.layout.support_simple_spinner_dropdown_item,stop_names);
autoCompleteTextView.setAdapter(adapter);
return rootView;
}
Its working for me...You missed to set threshold
AutoCompleteTextView autoCompleteTextView;
ArrayAdapter<String> adapter;
String[] stop_names;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test, container, false);
stop_names = new String[]{"usa", "china", "russia", "bangladesh"};
autoCompleteTextView = rootView.findViewById(R.id.autoCompleteTextView2);
adapter = new ArrayAdapter<>(getActivity(), R.layout.support_simple_spinner_dropdown_item, stop_names);
autoCompleteTextView.setThreshold(1);
autoCompleteTextView.setAdapter(adapter);
return rootView;
}
Set in your xml file android:completionThreshold.
<AutoCompleteTextView
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:layout_weight="1"/>

Change Listview in a Fragment to horizontal without using RecyclerView

View fragment_view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<String> datalist2 = new ArrayList<>();
datalist2.add("FirstAid");
datalist2.add("Login");
datalist2.add("Profile");
datalist2.add("Ambulance");
datalist2.add("Medicine");
datalist2.add("News");
datalist2.add("HRM");
fragment_view = inflater.inflate(R.layout.fragment_menu_bar, container, false);
list_view_menu_adapter adapter2=new list_view_menu_adapter (getContext(),datalist2);
final ListView L2= (ListView) fragment_view.findViewById(R.id.menu_scroll_list);
L2.setAdapter(adapter2);
How can I change this list to horizontal currently it is showing me verticaly without RecyclerView if possible

Get ListView in Fragment class , get NullPointerException

My Fragment layout:
<RelativeLayoutLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/my_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayoutLayout>
In my Fragment class:
ListView myList;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//NullPointerException
myList = (ListView) getView().findViewById(R.id.my_list);
...
}
Why I get NullPointerException when trying to get the ListView ?
You are trying to access your Fragment's View before the View has been inflated.
onCreateView() is where you create the View, not where you should be accessing the Views (onViewCreated() is more appropriate for that).
Your onCreateView() should have something like this to inflate your View:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.my_fragment_layout, container, false);
// If you want to do something with the layout here, you can
return layout;
}
As I couldn't find any fragment layout file which returns View to onCreateView(). So I think you missed it.
Your code look like,
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.<layout_xml>, container, false); // You missed this line
myList = (ListView) v.findViewById(R.id.my_list); // Now access myList from View v
return v;

Using findViewById inside a Fragment

I am creating a new Tab interface and for every tab I have functionality like:
public class ClassViewFragment extends Fragment {
ListView classListView = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.class_view, container, false);
classListView = (ListView) findViewById(R.id.classList);
return rootView;
}
}
But I am getting cannot resolve method findViewById also at another place I am also unable to use runOnUiThread and getting Error:(88, 29) error: cannot find symbol method runOnUiThread(<anonymous Runnable>)
I understand these will work fine if my class was extended from Activity but it’s a tab fragment so how can I use this funcitons?
you need
classListView = (ListView) rootView.findViewById(R.id.classList);
as that is the view you are concerned with
to get runOnUiThred you need to do getActivity().runOnUiThread
You need to reference findViewById using the view,
public class ClassViewFragment extends Fragment {
ListView classListView = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.class_view, container, false);
classListView = (ListView) rootView .findViewById(R.id.classList);
return rootView;
}
}
For runOnUiThread is a possible duplicate of Modifying an Android view from a different thread
use its like,
activity.runOnUiThread(new Runnable());
Here is a shore explanation of findViewById method;
findViewById() method will work with the related object that your main layout is bound to. For example, In an activity, when you make a
setContentView(activity_main);
You imply that your Activity will retrieve its layouts from activity_main. So when you make a findViewById() in an activity, that actually means this.findViewById(), where this is your Activity.
So in your case, the layout class_view is bound to the rootView. But when you just write findViewById, it means that this.findViewById. And this is the scope you're in, which is the Fragment in your case. However, I don't think a Fragment has that capability. So you should refer to the rootView to retrieve your views.
rootView.findViewById()
Fragments don't have a findViewById() method like Activity, so you have to call the method of the root view of the fragment. Try doing rootView.findViewById(R.id.classList) instead.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_content_mediaplayer, container, false);
mSeekbar = (SeekBar) v.findViewById(R.id.seekBarMusic);
return v;
Use rootView.findViewById(R.id.classList) instead of findViewById(R.id.classList).
Try this:
public class ClassViewFragment extends Fragment {
ListView classListView = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.class_view, container, false);
classListView = (ListView) rootView.findViewById(R.id.classList);
return rootView;
}
}
findViewById must run inside onViewCreated() method
ListView classListView = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.class_view, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
classListView = (ListView) view.findViewById(R.id.classList);
}
}`

Add data to a listview on fragment

I have an fragment on activity, with an listview element on it. Can't add data to listview. Can someone tell me what is wrong?
public class MainActivity extends FragmentActivity {
public String[] listS ={"item1","item2","item3"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MainFragment f = new MainFragment();
ft.replace(R.id.fragcontainer, f);
ft.commit();
//here the problem occurs
ListView lv = (ListView)findViewById(R.id.listf);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row,R.id.labeld, listS));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Fragment's class:
public class MainFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sis){
return inflater.inflate(R.layout.fragment_layout, container, false);
}
}
There's a number of problems with this method, but the root of it is that the FragmentTransaction replace() method that adds the fragment that, I assume, contains the ListView you're trying to find is an asynchronous process. The fragment isn't added to the Activity's view hierarchy immediately but some time later.
If you want to add things to a ListView in a Fragment, why not do it from within the MainFragment subclass' onCreateView() method instead? That's how it's intended to work.
EDIT: A typical onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
ListView lv = (ListView) view.findViewById(R.id.listf);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row,R.id.labeld, listS));
return view;
}
When u add code for define your class,
public class MainFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sis) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
ListView listView = (ListView) view.findViewById(R.id.list);
return view;
}
}
The problem is that you are asking to the activity for that ListView
"this."findViewById(...
You can ask the fragment for the view, or managing the content of the listView inside your Fragment class.
Please use the list view in the design windows as below. I faced the exact problem and now i could able to find the view in source code.
The main issue is IDE is appending "android" infront of ID
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

Categories

Resources