I am trying to retrieve the data from mysql and sort it based on the category tab. My application crash when i open the activity with content view not yet created. May I know wh
You can't call getListView()
in the onCreateView()
because the ListView doesn't exist yet. You need to put that code inside onViewCreated()
.
You can avoid crash by using check
if(this.isAdded())
if you debug that part BooksFragment.getListView int the moment of exception - you will notice it's not exist.
onCreateView
must only return the view that represent the fragment. Other operations on the View should be performed in another callback, onViewCreated
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_fragment, container, false);
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
productsList = new ArrayList<Products>();
lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
String description = ((TextView) view.findViewById(R.id.des))
.getText().toString();
String price = ((TextView) view.findViewById(R.id.budget))
.getText().toString();
String date = ((TextView) view.findViewById(R.id.date_posted))
.getText().toString();
String category = ((TextView) view.findViewById(R.id.category))
.getText().toString();
String email = ((TextView) view
.findViewById(R.id.email_request)).getText().toString();
String contact = ((TextView) view
.findViewById(R.id.contact_request)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getActivity(), DisplayItemInfo.class);
// sending information to next activity
in.putExtra(TAG_PID, pid);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_DES, description);
in.putExtra(TAG_BUDGET, price);
in.putExtra(TAG_DATE_POSTED, date);
in.putExtra(TAG_CATEGORY, category);
in.putExtra(TAG_EMAIL, email);
in.putExtra(TAG_CONTACT, contact);
startActivity(in);
}
});
new LoadAllProducts().execute();
}