Reputation: 103
Hi I'm having difficulty solving this error
this is the error enter image description here
I am having an issue filling up the listview in my fragment. My fragment is a tab and I am assuming this may be where my issue is.
I hope you can help me Thank you guys
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Mark Angelo on 1/13/2016.
*/
public class ListFragment extends Fragment {
TextView code,name,quantity,total;
ListView lv;
List<List_Items> myList = new ArrayList<List_Items>();
public ListFragment(){
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_fragment_layout,container,false);
populateList();
populateListView();
//lv = (ListView)getActivity().findViewById(R.id.client_listView);
return view;
}
private void populateList() {
myList.add(new List_Items("19912","Mark Angelo Bajaro", "1", "1993"));
myList.add(new List_Items("12812","Jonas Zaldua","3","18841"));
myList.add(new List_Items("2323", "Thor Sevilla", "1", "12121"));
}
private void populateListView() {
ArrayAdapter<List_Items> adapter = new MyListAdapter();
ListView list = (ListView)getActivity().findViewById(R.id.client_listView);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<List_Items> {
public MyListAdapter() {super(getActivity(), R.layout.list_content_layout, myList); }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemview = convertView;
if(itemview == null){
itemview = getActivity().getLayoutInflater().inflate(R.layout.list_content_layout,parent,false);
}
List_Items currentlist = myList.get(position);
code = (TextView)itemview.findViewById(R.id.code_contentent);
name = (TextView)itemview.findViewById(R.id.name_content);
quantity = (TextView)itemview.findViewById(R.id.quantity_content);
total = (TextView)itemview.findViewById(R.id.total_content);
code.setText(currentlist.getCode());
name.setText(currentlist.getClient_name());
quantity.setText(currentlist.getQuantity());
total.setText(currentlist.getTotal());
return itemview;
}
}
}
Upvotes: 0
Views: 8001
Reputation: 2509
Is your ListView inside the fragment layout? Or is it in the activity layout.
If it's in the fragment layout, try changing
ListView list = (ListView)getActivity().findViewById(R.id.client_listView);
to
ListView list = (ListView) view.findViewById(R.id.client_listView);
Hope that solves it.
Upvotes: 3
Reputation: 6307
NullPointerException happens when you try to call some function that has not been initialized, you just declared, try to see if id's are match if you initialzed by reference from xml layout or initialize
Upvotes: 0